我一直在玩一些JSON,试图获取并显示要在TextView中显示的字符串。 我可以在浏览器中访问此JSON元素,但不能在我的Android应用程序中访问。我得到一个执行,这里是logcat输出:
W/System.err: org.json.JSONException: End of input at character 0 of
W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:92)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
W/System.err: at com.bl.dbnewapp.ModelParser.parseFeed(ModelParser.java:17)
W/System.err: at com.bl.dbnewapp.MainActivity$MyTask.onPostExecute(MainActivity.java:103)
W/System.err: at com.bl.dbnewapp.MainActivity$MyTask.onPostExecute(MainActivity.java:89)
W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:636)
W/System.err: at android.os.AsyncTask.access$500(AsyncTask.java:177)
W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:135)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是JSON:
[{"id":"1","name":"john"},{"id":"2","name":"ben"}]
这是我的Model类:
public class Model {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是ModelParser类:
public class ModelParser {
public static List<Model> parseFeed(String content) {
try {
JSONArray ar = new JSONArray(content);
List<Model> modelList = new ArrayList<>();
for (int i = 0; i < ar.length(); i++) {
JSONObject obj = ar.getJSONObject(i);
Model model = new Model();
model.setName(obj.getString("name"));
modelList.add(model);
}
return modelList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
HttpManager:
public class HttpManager {
public static String getData(String uri) {
BufferedReader reader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String SO_URL = "http://www.example.com/webservice/?value=";
private static final String PARAMS = "[{%22table%22:%20%22names%22,%22operation%22:%20%22select%22}]";
TextView output;
List<MyTask> tasks;
List<Model> modelList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView) findViewById(R.id.textView);
output.setMovementMethod(new ScrollingMovementMethod());
tasks = new ArrayList<>();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_get_data) {
if (isOnline()) {
String URL = null;
try {
URL = URLEncoder.encode(PARAMS, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
requestData(SO_URL + URL);
} else {
Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show();
}
}
return false;
}
private void requestData(String uri) {
MyTask task = new MyTask();
task.execute(uri);
}
protected void updateDisplay() {
if (modelList != null) {
for (Model model : modelList) {
output.append(model.getName() + "\n");
}
}
}
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
private class MyTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
tasks.add(this);
}
@Override
protected String doInBackground(String... params) {
return HttpManager.getData(params[0]);
}
@Override
protected void onPostExecute(String result) {
modelList = ModelParser.parseFeed(result);
updateDisplay();
tasks.remove(this);
}
}
}
我会感激任何帮助,几乎尝试了所有这些工作,但无济于事。
答案 0 :(得分:0)
此问题已通过将%22
替换为\"
并删除PARAMS变量中的%20
来解决:PARAMS = "[{\"table\":\"names\",\"operation\":\"select\"}]"
感谢@keno的提升。