2015-11-04 56 views
-1

我想在多個開關櫃中發送請求,但每次調用請求時都會出現錯誤。請任何人幫助我!這裏是我的代碼:如何在開關櫃中發送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行的開關箱中。所以網址沒有打電話。任何人都有解決方案?

+0

請顯示錯誤日誌 –

+0

'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)' –

+0

錯誤在'httppost = new httPost(url)'開關的情況下' –

回答

0

廣東話你的URL =「192.168.137.121:1010/api/History」

+0

我已經試過這樣做了,但是不工作,它沒有從POST方法中取得url! –

+0

發表整個代碼 – danieljohngomez

+0

It是否有我發佈了!!! –

0

你能嘗試測試您的鏈接它的工作?您可以使用郵遞員測試您的發佈網址。

如果它工作正常,你可能想嘗試

URLEncoder.encode(URL);

String url =「192.168.137.121:1010/api/History」+ URLEncoder.encode(q,「UTF-8」);

+0

現在錯誤是運行時錯誤和'現在錯誤是'索引0的方案中的非法字符:192.168.137.121 :1010/API /歷史deviceMacAddres s = a8:1b:5a:b7:11:49&beaconMacAddress = [E7:15:2C:A0:05:21]' –

+0

你在網址前面有空白嗎? – John

+0

是的,我現在有'連接到http://192.168.137.121:1010拒絕'的錯誤! –