java - Android 与服务器的连接无法识别或引发错误。我究竟做错了什么?

标签 java android apache networking

我正在尝试通过 Android 应用程序将 JSON 数据推送到服务器,但无法隔离问题。服务器根本没有收到连接,但客户端没有表明任何问题。

public void getServerData() throws JSONException, ClientProtocolException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    ResponseHandler <String> resonseHandler = new BasicResponseHandler();
    HttpPost postMethod = new HttpPost("HTTP://URL_GOES_HERE/FOLDER");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    //Log.v("JSON being sent", json.toString());
    nameValuePairs.add(new BasicNameValuePair("jsonString", json.toString()));
    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    String response = httpClient.execute(postMethod,resonseHandler);

    JSONObject jsonResponse = new JSONObject(response);
    Log.v("beginresponse","beginresponse");
    Log.v("server response", jsonResponse.toString());
    Log.v("endresponse","endresponse");
    //Log.v("msg to server", json.toString());

}

这是一些示例日志输出:

10-20 11:31:09.195: I/APACHE HTTP (thCr=1) - NafHttpAuthStrategyDefault(25802): (thUse=1) NafHttpAuthStrategyDefault()
10-20 11:31:09.205: I/APACHE HTTP (thCr=1) - NafHttpAuthStrategyDefault(25802): (thUse=1)    cached value : gbaSupportIsPossible=null
10-20 11:31:09.205: I/APACHE HTTP (thCr=1) - NafHttpAuthStrategyDefault(25802): (thUse=1)    The current context is NOT a context of GBA service.
10-20 11:31:09.215: I/APACHE HTTP (thCr=1) - GbaSupportPermissionRequestCheckerImpl(25802): (thUse=1) isCurrentProcessRequestedGba()#finished   result=false
10-20 11:31:09.215: I/APACHE HTTP (thCr=1) - GbaSupportPermissionRequestCheckerImpl(25802): (thUse=1) isCurrentProcessAllowedToUseGba()#started   result=false
10-20 11:31:09.215: I/APACHE HTTP (thCr=1) - NafHttpAuthStrategyDefault(25802): (thUse=1)    The GBA permission wasn't requested for this process.
10-20 11:31:09.215: I/APACHE HTTP (thCr=1) - NafHttpAuthStrategyDefault(25802): (thUse=1) It is impossible to support GBA now (many possible reasons: no Android Context, current client is GBA service, etc.), then it will be just usual HTTP.
10-20 11:31:09.215: I/APACHE HTTP (thCr=1) - NafRequestExecutorWrapperRedirectionHandler(25802): (thUse=1)    It isn't GBA flow, redirection responses are not handled.
10-20 11:31:09.226: W/MediaRecorder(25802): mediarecorder went away with unhandled events
10-20 11:31:12.008: D/memalloc(25802): ion: Unmapping buffer  base:0x5ba42000 size:1536000
10-20 11:31:12.008: D/memalloc(25802): ion: Unmapping buffer  base:0x5bcb9000 size:1536000
10-20 11:31:12.008: D/memalloc(25802): ion: Unmapping buffer  base:0x5be30000 size:1536000
10-20 11:31:12.018: D/CLIPBOARD(25802): Hide Clipboard dialog at Starting input: finished by someone else... !
10-20 11:31:18.014: D/memalloc(25802): ion: Mapped buffer base:0x5ba42000 size:1536000 offset:0 fd:46
10-20 11:31:18.074: D/memalloc(25802): ion: Mapped buffer base:0x5bcb9000 size:1536000 offset:0 fd:53
10-20 11:31:23.439: D/memalloc(25802): ion: Mapped buffer base:0x5be30000 size:1536000 offset:0 fd:56
10-20 11:31:23.580: I/mic(25802): 1
10-20 11:31:23.580: I/path(25802): /mnt/sdcard/most-recent-recording.aac

最佳答案

是的,我猜从 API 11 开始,服务器连接将无法在 UI 线程上工作。所以尝试将其放入异步任务中。

关于java - Android 与服务器的连接无法识别或引发错误。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12990504/

相关文章:

java - 如何应用旋转的一部分

linux - 如何监控和记录 mt apache 网络服务器的 session

java - 2个表之间的Quarkus/Hibernate关系

android - 带有动态 Edittext 的 RecyclerView

android - 适用于 Android 的 OpenSL ES 教程

android - 多个 order by 和 null 值使用下一个字段排序,而不是第一个或最后一个

apache - 将支付信息发送到服务器的最佳方式是什么?

java - Android fragment 和 baseadapter 之间的通信

java - 在 java 中打印最多 9 位数的除法答案

java - Android 应用程序在空对象引用上出现登出 'boolean com.google.firebase.firestore.DocumentSnapshot.exists()' 问题