使用API​​客户端获取JSON对象

时间:2016-03-14 10:49:22

标签: java android json

我试图从api获取JSONobject,但我无法让这段代码工作。 我是android和java以及JSON的新手。我一直收到错误:在JSONobject中无法应用

主要代码:

try {
        APIClientJSONObject api = new APIClientJSONObject();
        JSONObject result = null;

        try {
            result = api.execute(URL).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<CustomListView> contents = new ArrayList<CustomListView>();

            try {
                JSONObject row = result.getJSONObject(result**ERROR HERE**);

                String content = row.optString("FormattedName");
                String content2 = row.optString("Title");
                String content3 = row.optString("Subtitle");
                String content4 = row.optString("Text");

                EditText name = (EditText) findViewById(R.id.etInternNaam);
                name.setText(content);
                EditText titel = (EditText) findViewById(R.id.etName);
                titel.setText(content2);
                EditText ondertitel = (EditText) findViewById(R.id.etOndertitel);
                ondertitel.setText(content3);
                EditText EditText = (EditText) findViewById(R.id.etTekst);
                EditText.setText(Html.fromHtml(content4));

            } catch (JSONException e) {
                e.printStackTrace();
            }

Api客户:

public class APIClientJSONObject extends AsyncTask<String, Void, JSONObject>             {

@Override
protected JSONObject doInBackground(String... params) {

    JSONObject result = null;

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
        InputStream inputStream = httpResponse.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        result = new JSONObject(builder.toString());
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    return result;
  }
}

JSON输出:

{
  "FormattedName": "Home page | Footer grijs",
  "Title": null,
  "Subtitle": null,
  "Text": "<div style=\"text-align: center;\"><img style=\"max-width: 80%;\" src=\"/MoxieManager/code.PNG\" alt=\"\"></div>",
  "WebsiteId": "6869a7a1-0d65-4cfa-9df1-b0b0d346212e",
  "Id": "b9906cb0-cdb2-484a-b603-020e8b64f97b",
  "DateCreated": "2016-01-25T12:09:50.367",
  "DateModified": "2016-02-11T08:51:54.223",
  "CreatedBy": "Drie-O Automatisering",
  "ModifiedBy": "Drie-O Automatisering",
  "SortOrder": 0
}

3 个答案:

答案 0 :(得分:0)

错误是否在这一行?

JSONObject row = result.getJSONObject(result);

result是一个JSONObject,该方法需要一个String。

为什么不尝试将结果转换为String并传递它。像。的东西。

JSONObject resultJson = new JSONObject();
   result.toString();
 JSONObject row = result.getJSONObject(resultJson );

答案 1 :(得分:0)

您已经获得了APIClient提供的JSON输出。

JSONObject row = result.getJSONObject(result);

这一行是多余的,除非您的实际响应是一个数组列表封闭对象,并且您只从它获取一行。

您可以直接访问主对象中的内部元素。

答案 2 :(得分:0)

<强>原因:

您已从JSONObject获得AsycnTask

没有必要
JSONObject row = result.getJSONObject(result);

当您尝试使用它时,这意味着您正在尝试在对象结果中查找对象结果。这不是这种情况。

<强>解决方案:

您应该删除上述呼叫并在以下呼叫中使用result

String content = result.optString("FormattedName");
String content2 = result.optString("Title");
String content3 = result.optString("Subtitle");
String content4 = result.optString("Text");