【问题标题】:get my restful service response null by using OKHttp使用 OKHttp 获取我的宁静服务响应 null
【发布时间】:2016-12-13 07:51:54
【问题描述】:

我是 android 新手,我的服务响应为空,但服务 URL 在浏览器中运行正常。当我运行我的应用程序屏幕时显示空白,并且没有数据来自我做错的服务,请帮助我。

我的片段:

public class SpecificCountryScholorshipsFragment extends Fragment  {

OKHttpRequest okHttpRequest = new OKHttpRequest();
ArrayList<ScholorshipsModel> arrayList;



ListView                        list
/***********************************************************************************/

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    try {

        String  getResponse = okHttpRequest.doGetRequest("http://192.168.100.7/scholar/web/app.php/scholarship");
        if (getResponse != null) {
             //Toast.makeText(getActivity(), getResponse, Toast.LENGTH_LONG).show();

            JSONArray jArray = new JSONArray(getResponse);

            for (int i = 0; i < jArray.length(); i++) {

                JSONObject jRealObject = jArray.getJSONObject(i);
                ScholorshipsModel model = new ScholorshipsModel();
                model.setScholorship_name(jRealObject.getString("name"));
                model.setScholorship_detail(jRealObject.getString("details"));
                model.setScholorship_pic(jRealObject.getString("picture"));

                arrayList.add(model);
                setAdapterValue();

            }


        }

        View view = inflater.inflate(R.layout.scholorship_listing_main, container, false);
        list = (ListView) view.findViewById(R.id.Scholorshiplist);



        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getActivity(), "List is clicked ", Toast.LENGTH_LONG).show();

                Intent intent = new Intent(getActivity(), ScholorshipDetailActivity.class);
                getActivity().startActivity(intent);
            }
        });

        return view;
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
return null;
}



    /***********************************************************************************/

private void setAdapterValue() {
    ScholorshipAdapter adapter = new ScholorshipAdapter(getActivity(),  R.layout.scholorship_listing, arrayList);
    list.setAdapter(adapter);
}
}

OKHttp 类:

public class OKHttpRequest {
OkHttpClient client = new OkHttpClient();

public static final MediaType JSON
        = MediaType.parse("application/json; charset=utf-8");


public String doGetRequest(String url) throws IOException {

    try {
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();

    }
    catch (Exception e){
        e.printStackTrace();
    }
   return null;

}



String doPostRequest(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}}

【问题讨论】:

  • 如果您使用的是 PHP 服务,并且您只是在此处回显语句,您会在浏览器上得到响应,但当您从移动设备调用 api 时却没有,因此请检查您的服务
  • 服务工作正常,但是当我从 android 调用它时,它给出空响应 @NoumanShah
  • 你没有得到响应表单服务,所以我认为错误应该在服务中

标签: android okhttp restful-url


【解决方案1】:

首先通过POSTMAN 检查您的 API,我认为您在 Hader 中缺少某些参数,或者由于响应不是来自服务器而可能在正文中。另外我推荐用户volly 进行客户端-服务器通信

【讨论】:

  • 在 POSTMAN 中我的服务运行正常 @DynamicMind
  • 尝试使用 volly 发送 JSon/String 请求然后检查然后将 log int 两个 onResponse 和 OnError 方法,我希望通过这种方式我们可以找到问题在哪里,客户端和服务器端
【解决方案2】:

您也可以使用 Rest-client(Chrome 和 Firefox 的附加组件),如果一切正常,请尝试使用 HttpsURLConnection,它可能会更好地帮助您。

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 2017-03-05
    • 1970-01-01
    • 2015-04-11
    • 2011-12-10
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多