android–http post方法将空值传递给服务器

android–http post方法将空值传递给服务器,第1张

概述try { url= new URL(ConstantsClass.VENDOR_FOLLOW + '?UID=' +android_id+'&URL=''+resultfinal+''&device=android'); connection = (HttpU

         try          {            url= new URL(ConstantsClass.vendOR_FolLOW + "?UID=" +androID_ID+"&URL='"+resultfinal+"'&device=androID");                connection = (httpURLConnection) url.openConnection();                connection.setDoOutput(true);                connection.setRequestMethod("POST");                request = new OutputStreamWriter(connection.getoutputStream());                request.flush();                request.close();                request.write("Hello!!!");                String line = "";                inputStreamReader isr = new inputStreamReader(connection.getinputStream());                BufferedReader reader = new BufferedReader(isr);                StringBuffer sb = new StringBuffer();                while((line=reader.readline())!=null) {                    sb.append(line + "&");                }                response = sb.toString();                //response.getEntity().getContent();                Log.i("Test","updated response: " + response);            }         catch (IOException e) {              e.printstacktrace();          }            Log.i("Test","**************url List********************" + url);         tag_text.setonClickListener(new OnClickListener() {                public voID onClick(VIEw v) {                    // Todo auto-generated method stub                    Intent in=new Intent(context,linkWebVIEw.class);                    in.putExtra("vendorUrl",resultfinal);                    context.startActivity(in);                          //postData();                }            });             }    tag_text.setTextSize(16);    return vIEw;}   

嗨,我是AndroID的新手,我试图将值从url传递服务器,但我得到在服务器端传递的空值.更新响应为空.我的服务器端值不给我任何值.我需要从上面给出的url传递url,androID_ID和device.我也尝试了httpclIEnt,但它也给了我null值.最佳答案你应该尝试下面的代码,它运行得很好.

   // ADD YOUR REQUEST DATA HERE  (you can pass number of variable).    ArrayList

现在建立你的网络连接

(1)将简单字符串发送到服务器

    try    {        httpClIEnt httpclIEnt = new DefaulthttpClIEnt();        httpPost httppost = new httpPost("your url only ex:www.Google.com/abc");        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        httpParams httpParameters = new BasichttpParams();        DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt(httpParameters);        httpResponse response = httpclIEnt.execute(httppost);        httpentity entity = response.getEntity();        is = entity.getContent();    } catch (Exception e)     {           Log.e("Loading Runnable Error in http connection  :",e.toString());    }

(2)将JsON编码字符串发送到服务器

httpClIEnt clIEnt = new DefaulthttpClIEnt();httpconnectionParams.setConnectionTimeout(clIEnt.getParams(),10000); //Timeout limithttpResponse response;JsONObject Json = new JsONObject();try {httpPost post = new httpPost(URL);Json.put("user_name","chintan");Json.put("password","khetiya");StringEntity se = new StringEntity( Json.toString());  se.setContentType(new Basic@R_419_5548@(http.CONTENT_TYPE,"application/Json"));post.setEntity(se);response = clIEnt.execute(post); /*Checking response */if(response!=null){is = response.getEntity().getContent(); //Get the data in the entity }} catch(Exception e) {e.printstacktrace();createDialog("Error","Cannot Estabilish Connection");}

两种情况下的反应都相同

try     {        BufferedReader reader = new BufferedReader(new inputStreamReader(is,"iso-8859-1"),8);        StringBuilder sb = new StringBuilder();        String line = null;        while ((line = reader.readline()) != null)         {            sb.append(line + "\n");        }        is.close();        result = sb.toString();    }    catch (Exception e)     {           Log.e("Loading Runnable Error converting result :",e.toString());    }

现在最终结果包含整个输出字符串,现在它取决于您将如何读取数据.使用Json或者其他.我正在使用Json,所以把它的示例代码可能对你有所帮助.

JsONObject Json_data = new JsONObject(result);// its a string var which contain output.         my_output_one = Json_data.getString("var_1"); // its your response var form web.        my_output_two = Json_data.getString("var_2");

现在它结束了你有两个变量,它有任何价值,并使用任何.

现在这对你有帮助.如果您有任何疑问,请告诉我.

总结

以上是内存溢出为你收集整理的android – http post方法将空值传递给服务器全部内容,希望文章能够帮你解决android – http post方法将空值传递给服务器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1139667.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)