问题描述
- http post方法传递到服务器null值
-
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(contextLinkWebView.class); in.putExtra(""vendorUrl"" resultfinal); context.startActivity(in); //postData(); } }); } tag_text.setTextSize(16); return view;}
我想把一个值从url中传递到服务中,但是在服务这边获得是null值。重新响应后server依然还是null值。我想传递代码中给出的url中的url、android_id和device。使用httpclient也还是返回的null值。谁知道怎么传递这个值呢?
解决方案
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair(""Your_var_1"" value)); nameValuePairs.add(new BasicNameValuePair(""Your_var_2"" value));
然后再创建你的web链接
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()); }
现在检索响应流
try { BufferedReader reader = new BufferedReader(new InputStreamReader(isiso-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示例代码希望能帮得上你啊。
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"");
解决方案二:
代码贴出来 估计你在处理数据的时候没装进去 你这样说很难知道为什么
解决方案三:
有代码才能知道你写的哪里有问题啊。不然说的很空洞,不能详细。
时间: 2024-12-21 09:25:53