HTTP POST方法传递null值到服务器服务器、方法、HTTP、POST

由网友(亲情交流地)分享简介:try {url= new URL(ConstantsClass.VENDOR_FOLLOW + "?UID=" +android_id+"&URL='"+resultfinal+"'&device=android");connection = (HttpURLConnection) url.open...
         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;

}   

我是新来的Andr​​oid和我试图从URL到服务器传递价值观,但我得到的服务器端传递空值。更新响应是给空。我的服务器端的值不给任何值给我。我需要传递的网址,android_id和设备从上面给出的URL。我尝试了HttpClient的还,但它给了我空值的这一点。

Hi i am new to android and I am trying to pass values from the url to the server but i am getting null values passed on the server side. Update response is giving null. My server side values dont give any values to me. I need to pass the url, android_id and device from the url that is given above. I tried the httpclient also but it gives me null values in that as well.

推荐答案

你应该尝试code以下它的运行对我非常好。

you should try below code its running very well for me.

   // ADD YOUR REQUEST DATA HERE  (you can pass number of variable).
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("Your_var_1", value));
    nameValuePairs.add(new BasicNameValuePair("Your_var_2", value));

现在建立像

    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恩code字符串到服务器

HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse 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 BasicHeader(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这样就把例如code它可能对你有所帮助。

Now at the end result contain whole output string now its depend on you how you will read your data. using json or else. i am doing using json so put example code of it may be helpful to you.

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");

现在其对你有两个变量的一种具有任何价值和使用的任何人。

Now its over you have two variable which having any kind of value and use any were.

现在这将帮助你。如果您有任何疑问,让我知道。

Now this will helpful to you. if you have any query let me know.

阅读全文

相关推荐

最新文章