2 znl 12 znl_12 于 2012.11.07 14:01 提问

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(context,LinkWebView.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值。谁知道怎么传递这个值呢?

3个回答

hxn_217
hxn_217   2012.11.07 16:40
已采纳
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(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示例代码,希望能帮得上你啊。

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");
KPRF2009
KPRF2009   2012.11.07 14:36

代码贴出来 估计你在处理数据的时候没装进去 你这样说很难知道为什么

songhait
songhait   2012.11.07 15:00

有代码才能知道你写的哪里有问题啊。不然说的很空洞,不能详细。

znl_12
znl_12 可以在评论中说哦~~
大约 5 年之前 回复
znl_12
znl_12 谢谢你的提醒,代码加了。欢迎解答我的问题。
大约 5 年之前 回复
hooopo
hooopo 是的啊...
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!