如何在android中调用带有post数据的webservice

时间:2013-04-24 13:52:07

标签: java android web-services

我没有得到任何回复,我的代码中有什么问题。由于隐私政策,我没有提供正确的URl。

public static String URL = "http://www.example.com/web_service/mainAPI.php";
final String body = String.format("rquest={\"method\":\"upcoming_shows\",\"body\":[{}]}");
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.upcoming_show);
    try{    
        Log.d("In Try", "Going...");
        final HttpClient client = new DefaultHttpClient();
        final HttpPost postMethod = new HttpPost(URL);
        postMethod.setEntity(new StringEntity(body, "utf-8"));
        final HttpResponse response = client.execute(postMethod);
        final String responseData = EntityUtils.toString(response.getEntity(), "utf-8");
        Toast.makeText(UpcomingShow.this, responseData, Toast.LENGTH_SHORT).show();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

请给我一些解决方案。

1 个答案:

答案 0 :(得分:2)

  1. 您正在尝试访问UI线程上的网络,不再允许这样做。见here。和here
  2. 你的String body中可能有拼写错误,它说rquest,我认为它应该是request(第2行)