1
我正在使用HTTPPost將值發送到服務器。該腳本適用於我下面的內容。但是,如果我改名字值對這個我得到一個強制關閉:Android NameValuePair不接受字符串
nameValuePairs.add(new BasicNameValuePair("id", idOfTextView.getText().toString()));
這是沒有它讀取的TextView代碼:
String url_select = "http://mydomain.com/get.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("id", "1"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
//read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
logcat中的錯誤是什麼?另外,確保'idOfTextView'非空。 – wsanville
引起:java.lang.NullPointerException> 06-14 15:27:36.606:E/AndroidRuntime(29275):at com.sample.sampleapp.viewRandom $ task.doInBackground(viewRandom.java:78) 78行將是我嘗試設置名稱值對的行。 –
然後,根據您的錯誤日誌 – Guardanis