【问题标题】:Ion son unable to parse Json from localhost离子儿子无法从本地主机解析 Json
【发布时间】:2016-01-09 23:53:27
【问题描述】:

我在使用 android studio 中的一段代码时遇到了问题,该代码已经可靠运行了数周。 每当我尝试通过 localhost 接收 Json(用于在模拟器或我的设备上调试并通过 wifi 连接)时,我都会收到错误消息:

com.google.gson.JsonParseException: 无法解析 json

但是,如果我连接到远程服务器以获取 JSON Echo,例如:

http://echo.jsontest.com/insert-key-here/insert-value-here/key/value

没有问题。

我已经验证 JSON 是有效的,就像我说它以前对我有用,但无论如何我已经在网上仔细检查了它,我什至创建了一个新的基本应用程序,只有一个按钮和一个请求,但仍然有同样的问题。

代码如下:

    public static class PlaceholderFragment extends Fragment {

    public static final String ACCOUNT_VIEW_URL = "http://192.168.0.10/~user/tests/accountEcho.php";  //"http://echo.jsontest.com/insert-key-here/insert-value-here/key/value";
    Button button;

    private static final String ARG_SECTION_NUMBER = "section_number";


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home_screen, container, false);
        button = (Button) rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netWork();
            }
        });
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((HomeScreenActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }


    void netWork() {
        Ion.with(getActivity())
                .load(ACCOUNT_VIEW_URL)
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {

                        if (null == e) {
                            //   Log.v("onCompleted", "not null");
                            Log.v("result", ""+result+"");
                        } else {
                            //  Log.v("onCompleted", "null");
                            Log.v("on completed", "" + e + "");

                        }
                        //TODO:::: JSON STRUCTURE - LOAD DP INTO VIEW
                    }
                });

    }
}

}

JSON 响应是:

{
        "accType": "2",
        "firstname": "duracell",
        "lastname": "battery"
        "house_num": "122",
        "addL1": "Brick Lane",
        "addL2": "",
        "city": "Middx"

}

此外,如果我从计算机上的任何其他应用程序(visualJSON、chromeAdvancedRestClient、safari、firefox 等)访问相同的本地主机地址,它工作正常并且响应符合预期。

谁能告诉我这是怎么回事?

谢谢

【问题讨论】:

    标签: android json android-studio gson ion


    【解决方案1】:

    如果您的 JSON 在服务器上,并且您通过 HTTP 请求访问它,则只需解析内容,而不是带有标头的整个文件。

    尝试做一个 HTTP 客户端,使用 getResponse.getEntitygetResponse.getContent,然后在其中之一上使用您的解析器。

    我从here复制了那个。

    【讨论】:

      【解决方案2】:

      我就是这样解决这个问题的:

      第 1 步:

      确保您有一个 object 类,objects 必须与您的 JSON 对象具有相同的名称。

      public class Tester {
      
          private String accType;
          private String firstname;
          private String lastname;
          private String addL1;
          private String addL2;
          private String city;
      
      
          public String getAccType() {
              return accType;
          }
      
          public void setAccType(String accType) {
              this.accType = accType;
          }
      
          public String getFirstname() {
              return firstname;
          }
      
          public void setFirstname(String firstname) {
              this.firstname = firstname;
          }
      
          public String getLastname() {
              return lastname;
          }
      
          public void setLastname(String lastname) {
              this.lastname = lastname;
          }
      
          public String getAddL1() {
              return addL1;
          }
      
          public void setAddL1(String addL1) {
              this.addL1 = addL1;
          }
      
          public String getAddL2() {
              return addL2;
          }
      
          public void setAddL2(String addL2) {
              this.addL2 = addL2;
          }
      
          public String getCity() {
              return city;
          }
      
          public void setCity(String city) {
              this.city = city;
          }
      }
      

      第 2 步:

      然后我以列表的形式检索 JSON 数据,但如果假设我有一个项目,我只需索引每个元素,如下所示

              Ion.with(getBaseContext())
                              .load(url_)
                              .progressDialog(pd)
                              .as(new TypeToken<List<Tester>>() {
                              })
                              .setCallback(new FutureCallback<List<Tester>>() {
      
                                  @Override
                                  public void onCompleted(Exception e, List<Tester> itemList) {
      
                                      try {
      
      
                                          String firstname= itemList.get(0).getFirstname();
      
      //DO the same to other elements
      
            Log.v("result", "Firstname :"+firstname+"");
      
      
                                          pd.dismiss();
      
      
                                      }
      
                                      catch (Exception ex){
      
                                          Toast.makeText(getApplicationContext(),"Please check your Internet connection",Toast.LENGTH_LONG).show();
                                      }
      
      
      
      
      
      
                                  }
      
      
      
                              });
      
      
              }
      

      【讨论】:

        猜你喜欢
        • 2013-11-28
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-15
        相关资源
        最近更新 更多