将 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>
输出::
获得的上述代码的输出是
接下来我将我的网址更改为
URL:: http://54.218.73.244:7003/
并且无需更改代码的其他部分
现在我得到错误屏幕
错误日志
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
- 如何解决这个错误?
- 对于网址 ::
http://54.218.73.244:8084/
我得到的 JSON 回复是:: [{"id":1,"content":"Hello"},{"id":2,"content":"World"}]
- 对于网址 ::
http://54.218.73.244:7003/
我得到的 JSON 回复是::[{"id":1,"content":"Hello"},{"id":2,"content":"World"}]
JSON的结构相同,请在浏览器中使用url确认
谢谢,