程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-http post方法傳遞到服務器null值

android-http post方法傳遞到服務器null值

編輯:編程綜合問答
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值。誰知道怎麼傳遞這個值呢?

最佳回答:


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");
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved