Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用Android异步http库从API web获取到Android的json响应_Android_Json_Api_Android Async Http - Fatal编程技术网

尝试使用Android异步http库从API web获取到Android的json响应

尝试使用Android异步http库从API web获取到Android的json响应,android,json,api,android-async-http,Android,Json,Api,Android Async Http,我一直在android上使用android async http()lib,但由于某些原因,我无法捕获服务器的响应,我知道服务器接收并执行应该执行的操作,但我无法无缘无故地获得响应 以下是调用API的方法: public User registUser(String mail, String pass) throws UnsupportedEncodingException { final User user = new User(); user.setToken("enter"

我一直在android上使用android async http()lib,但由于某些原因,我无法捕获服务器的响应,我知道服务器接收并执行应该执行的操作,但我无法无缘无故地获得响应

以下是调用API的方法:

public User registUser(String mail, String pass) throws UnsupportedEncodingException {
    final User user = new User();
    user.setToken("enter");

    String bodyAsJson = "{\"user\":{\"email\":\""+mail+"\",\"password\":\""+pass+"\"}}";

    StringEntity entity  = new StringEntity(bodyAsJson);
    Header[] headers = {
            new BasicHeader("Content-type", "application/json")
    };

    client.post(this.context, "http://104.131.189.224/api/user", headers , entity, "application/json",  new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject json) {
            try {
                json = json.getJSONObject("user");
                user.setId(json.getInt("id"));
                user.setEmail(json.getString("email"));
                user.setPassword("123456");
                user.setToken(json.getString("auth_token"));
            } catch ( JSONException e) {
                user.setToken("not json");
            } catch (Exception e) {
                user.setToken("error ");
            }
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
            user.setToken("comes json array");
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            user.setToken(responseString);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            user.setToken("error");
        }

        @Override
        public void onRetry(int retryNo) {
            user.setToken("nothing");
        }
    });

    return user;
}
当我调用该方法时,user.getToken仅显示我在beginin中输入的“enter”,这意味着从未在onSuccess、onFailure或onRetry方法中输入

但我知道服务器收到我的请求,因为服务器日志显示: (例如:电子邮件:carlos@prueba.com,通过:普鲁巴)

服务器应以以下格式响应json:

{"user":{"id":3,"email":"carlos@prueba.com","auth_token":"dc45800fddee07cf9b300d2765283cb2"}}

大多数教程都过时了,尝试使用apache库,但我确实找到了一个可用的

在尝试操纵团队树屋教程()以使用事件查找器api而不是天气api时,我遇到了同样的问题

他们使用OkHttp库

  • 将OkHttp库编译到Module:app下的
    build.gradle
    文件中
  • 编译'com.squareup.okhttp:okhttp:2.4.0'

    这是截至2015年7月9日的最新版本

  • 访问api url,查看您是否确实在检索数据,以及数据的格式是否正确。对我来说,我在使用Eventful.com,并在我所在的地区查找事件。 ()我的应用程序密钥将位于url末尾的“=”后面
  • 在这一点上,一切都很好,现在我需要添加OkHttp代码来下载json数据

  • 将internet权限添加到清单中
  • 
    

  • 打开将发出请求的类或活动,并添加以下代码
  • String eventfulUrl=”https://api.eventful.com/json/events/search?l=arizona&within=15&units=miles&app_key=“+apiKey;
    OkHttpClient=新的OkHttpClient();
    Request Request=newrequest.Builder()
    .url(eventfulUrl)
    .build();
    

    然后打电话

    ` 
    Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
    
            }
    
            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    if (response.isSuccessful()) {
                        Log.v(TAG, response.body().string());
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Exception Caught: ", e);
                }
            }
        });
    
    所有这些都添加到了
    onCreate()

    这条线
    Log.v(TAG,response.body().string())
    是将响应输出到日志的内容,并且位于if语句中,您可以在其中处理数据


    如果url不是以“http://”或“https://”开头,代码将不会得到响应,但是应用程序仍将运行无错误

    从服务器端提供代码。我没有服务器端的代码,但让我问一下我是否可以得到它我认为服务器端代码是问题所在,我会要求它,但如果你阅读日志,服务器响应代码为200,那么为什么不在我的代码中输入一些onSuccess方法呢?为了澄清错误,我建议您尝试(1)
    AsyncHttpResponseHandler
    ,而不是JsonHttpResponseHandler。检查服务器响应更简单。(2) 使用Fiddler查看实际的http通信。它是调试http通信的极好工具。同样,从未输入onscuiss或其他任何方法
    ` 
    Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
    
            }
    
            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    if (response.isSuccessful()) {
                        Log.v(TAG, response.body().string());
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Exception Caught: ", e);
                }
            }
        });