gpt4 book ai didi

android - 将简单的 JSON 解析为 android

转载 作者:搜寻专家 更新时间:2023-11-01 08:57:17 26 4
gpt4 key购买 nike

在将 JSON 数据解析为 android 时遇到问题

  • 我已经描述性地提到了我面临的问题,关于如何克服这个问题

最初我使用来自 url 的 JSON

URL::http://54.218.73.244:8084/

JSONParser.java

public class JSONParser {

static InputStream is = null;
static JSONArray jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONArray getJSONFromUrl(String url) {

// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;

}
}

AndroidJSONParsingActivity.java

    public class AndroidJSONParsingActivity extends Activity {

// url to make request
private static String url = "http://54.218.73.244:8084/";
private HashMap<Integer, String> contentsMap = new HashMap<Integer, String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.on

Create(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();

// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);

try {
for (int i = 0; i < json.length(); i++) {
JSONObject c = json.getJSONObject(i);

// Storing each json item in variable
int id = c.getInt("id");
String name = c.getString("content");

contentsMap.put(id, name);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
protected void onResume() {
super.onResume();

TextView textView=(TextView) findViewById(R.id.textView1);
textView.setText(contentsMap.get(1));

textView=(TextView) findViewById(R.id.textView2);
textView.setText(contentsMap.get(2));


}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10sp">

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10sp"
android:text="TextView1" />

<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10sp"
android:text="TextView2" />

</LinearLayout>

输出::

以上代码得到的输出是

FIG-1


接下来我将我的 URL 更改为

网址::http://54.218.73.244:7003/

并且不改变代码的其他部分

现在我得到错误屏幕

FIG-2


错误日志

08-10 09:45:41.731: E/JSON Parser(448): Error parsing data org.json.JSONException: Value Cannot of type java.lang.String cannot be converted to JSONArray
08-10 09:45:41.737: D/AndroidRuntime(448): Shutting down VM
08-10 09:45:41.737: W/dalvikvm(448): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-10 09:45:41.772: E/AndroidRuntime(448): FATAL EXCEPTION: main
08-10 09:45:41.772: E/AndroidRuntime(448): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.json_web/com.example.json_web.AndroidJSONParsingActivity}: java.lang.NullPointerException
08-10 09:45:41.772: E/AndroidRuntime(448): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-10 09:45:41.772: E/AndroidRuntime(448): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-10 09:45:41.772: E/AndroidRuntime(448): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-10 09:45:41.772: E/AndroidRuntime(448): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)

我根据错误分析::

  • 解析字符串时出现错误无法转换为JSONArray
  • 如何解决这个错误?

  • 对于 URL::http://54.218.73.244:8084/

我得到的 JSON 回复是::[{"id":1,"content":"Hello"},{"id":2,"content":"World"}]

  • 对于 URL::http://54.218.73.244:7003/

我得到的 JSON 回复是::[{"id":1,"content":"Hello"},{"id":2,"content":"World"}]

JSON 结构相同,请使用浏览器中的 url 确认这一点


谢谢,


最佳答案

您位于 http://54.218.73.244:7003/ 的服务器不支持 POST 请求。

因此,要么让您的服务器接受 POST 请求,要么更改 JSONParser 类中的以下行。

//HttpPost httpPost = new HttpPost(url); //Remove this and replace with the below

HttpGet httpGet = new HttpGet(url);

关于android - 将简单的 JSON 解析为 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18158642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com