[英]JSON data Parsing Android Studio
我目前是android编程的初学者,现在我正在开发一个应用程序,以建立与服务器的JSON连接并显示JSON数据。 目前已成功建立连接,但是在我的应用程序上显示JSON数据时遇到问题。 我的JSON数据采用以下格式`
我首先在我意识到不正确的地方使用JSONObject而不是JSONArrays,所以我认为我已经正确更改了这些,但是现在在MainActivity中,我对如何在应用程序上显示来自JSON连接的数据感到困惑。 在这里的任何帮助,将不胜感激。 我当前的错误出现在所有三个Node_所在的MainActivity中。
错误:(46,25)错误:类JSONArray中的方法getJSONArray无法应用于给定类型; 必需:找到的整数:字符串原因:实际参数字符串不能通过方法调用转换转换为整数
我还将在下面包括我的课程:
MainActivity类
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends ActionBarActivity {
//URL to get JSON Array
private static String url = "";
//JSON Node Names
private static final String Node_ID = "id";
private static final String Node_BusinessName = "BusinessName";
JSONArray array = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONArray json = jParser.getJSONFromUrl(url);
try {
// Getting JSON Array
array = json.getJSONArray(Node_BusinessName);
JSONArray c = array.getJSONArray(0);
// Storing JSON item in a Variable
String id = c.getString(Node_ID);
String businessName = c.getString(Node_BusinessName);
//Importing TextView
final TextView ID = (TextView)findViewById(R.id.ID);
final TextView BusinessNameText = (TextView)findViewById(R.id.BusinessName);
//Set JSON Data in TextView
ID.setText(id);
BusinessNameText.setText(businessName);
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONParser类
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
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;
}
}
您的问题在这里。
JSONArray json = jParser.getJSONFromUrl(url);
try {
array = json.getJSONArray(Node_BusinessName);
JSONArray c = array.getJSONArray(0);
您的第一个标签是JSONArray
并且在JSONParser
类中已使用JSONArray
返回。 所以只要删除这行
array = json.getJSONArray(Node_BusinessName);
并使用像
for(int i=0; i<json.length();i++){
JSONObject jb = json.getJSONObject(i);
String id = jb.getString(Node_ID);
// same for other keys
}
注意:您不建议在主线程上执行与网络相关的操作。 因此,请使用AsyncTask
或Thread
。
首先,我认为您的json文本已损坏。 如果问题与转换数据有关,则可以使用Java代码将其转换为字符串或对象后再获取。
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
尝试这个。
并尝试将此JSON代码放入Async Task中,这样会更有效。
尝试点击此链接 。 您将学到更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.