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