2015-11-04 37 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拒绝'的错误! –