如何在Switch Case

时间:2015-11-04 13:19:19

标签: java android http-post httprequest

我想在多个交换机案例中发出请求,但在调用请求时每种情况都会出错。请有人帮帮我!这是我的代码:

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行的switch case中。所以网址没有接到电话。有人有解决方案吗?

2 个答案:

答案 0 :(得分:0)

你不能做url =&#34; 192.168.137.121:1010 / api / History&#34;

答案 1 :(得分:0)

您可以尝试测试您的链接是否正常工作?您可以使用邮递员来测试您的帖子网址。

如果工作正常,您可能想尝试

  

URLEncoder.encode(URL);

     

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