java - 如何在android中使用post数据调用webservice

标签 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. 您正在尝试在 UI 线程上访问网络,这不再被允许。请参阅here 。和 here .
  2. 您的 String body 中可能有拼写错误,上面写着rquest ,我相信应该是request (第 2 行)

关于java - 如何在android中使用post数据调用webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16193801/

相关文章:

android - 如何在 JMeter 中测试 Android 应用程序?

Android 服务通信,通过 SSL 使用原始字节

ios - 在 iOS 上使用 SOAP Web 服务

java - 比较 Java 7 中的 Object 和 int

java - 将静态方法应用于存储在子类中的数据

android - FusedLocation API - 未调用 onLocationChanged

android - 在运行时更改 VideoView 的大小

java - 由于 StandardLockService 中 Oracle 上的 Liquibase 问题,Spring-Boot 应用程序无法启动

使用 mozilla webclient 的 java 浏览器

asp.net - 可能的 SOAP 版本不匹配