http post方法将空值传递给服务器
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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("Your_var_1", value)); nameValuePairs.add(new BasicNameValuePair("Your_var_2", value));
现在build立你的networking连接
(1)发送简单的string到服务器
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编码string发送到服务器
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()); }
现在最后的结果是包含整个输出string,现在取决于你如何读取数据。 使用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");
现在它超过了你有两个variables有任何价值和使用任何一种。
现在这将有助于你。 如果你有任何疑问让我知道。
写完后请调用flush,并在finally块中closuresstream。 看看下面的代码:
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.write("Hello!!!"); request.flush(); 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; }finally{ try{ request.close(); }catch(Exception e){} }