我想在多個開關櫃中發送請求,但每次調用請求時都會出現錯誤。請任何人幫助我!這裏是我的代碼:如何在開關櫃中發送POST請求
public class BeaconListAdapter extends BaseAdapter {
private ArrayList<Beacon> beacons;
private LayoutInflater inflater;
private Context context;
public HttpPost httpPost;
public BeaconListAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
this.beacons = new ArrayList<>();
this.context= context;
}
if (beacon.getRssi() < -20 && beacon.getRssi() > -85) {
String query = "?deviceMacAddress=" + info.getMacAddress() + "&beaconMacAddress=" + maxBeacon.getMacAddress();
// new HttpAsyncTask().execute("http://113.20.19.234:92/api/Beacon/Get" + query);*/
fbmajor= maxBeacon.getMajor();
switch (fbmajor) {
case 1333:
Intent intent = new Intent(context, I1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
new HttpAsyncTask().execute("http://192.168.137.121:1010/api/History" + query);
Log.i("Call", query);
HttpPost httpPost = new HttpPost(url);
Log.e("Tag11",URL);
break;
case 51185:
Intent intent2 = new Intent(context, I2.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
new HttpAsyncTask().execute("http://192.168.137.121:1010/api/History" + query);
Log.i("Call", query);
httpPost = new HttpPost(url);
break;
case 51292:
Intent intent3 = new Intent(context, I3.class);
intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent3);
new HttpAsyncTask().execute("http://192.168.137.121:1010/api/History" + query);
Log.i("Call", query);
httpPost = new HttpPost(url);
Log.e("Tag3", URL);
break;
case 14948:
Intent intent4 = new Intent(context, I4.class);
intent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent4);
new HttpAsyncTask().execute("http://192.168.137.121:1010/api/History" + query);
Log.i("Call", query);
httpPost = new HttpPost(url);
Log.e("Tag4", URL);
break;
}
}
public String POST(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpPost(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
//Toast.makeText(getBaseContext(), "POST!", Toast.LENGTH_LONG).show();
//etResponse.setText(result);
}
}
這裏的錯誤出現在http Post行的開關箱中。所以網址沒有打電話。任何人都有解決方案?
請顯示錯誤日誌 –
'11 -04 19:28:30.019 15145-15145/com.estimote.examples.demos E/AndroidRuntime:致命異常:主 工藝:com.estimote.examples.demos,PID :15145 java.lang.IllegalArgumentException at org.apache.http.client.methods.HttpPost。(HttpPost.java:82) at com.estimote.examples.demos.adapters.BeaconListAdapter.bind(BeaconListAdapter.java:207) at com.estimote.examples.demos.adapters.BeaconListAdapter.getView(BeaconListAdapter.java: 139) at android.widget.AbsListView.obtainView(AbsListView.java:2353)' –
錯誤在'httppost = new httPost(url)'開關的情況下' –