Java 字符串到 json 对象
Java string to json object
我正在尝试将 Json 与 android 一起使用
我有一个 link returns 一个 Json 字符串
InputStream is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
但是,在创建一个新的 JSONObject(jsonText) 时;它压碎了。
这是我的URL:http://igorsdomain.byethost3.com/getUser.php?username=SailorBoogy&password=qwerty&event=2222
这是返回的对象:{"ID":"23","Username":"SailorBoogy","Password":"qwerty","eventID":"6"}
我也试过 JSONObject(jsonText.replaceAll("\"" , "\\\""));
但是没用。
有什么问题吗?我用错了吗?
Json 图书馆:
org.json.JSONException;
org.json.JSONObject;
org.json.JSONStringer;
没有 LogCat 很难说,但可能你正在 MainThread 上进行网络操作,而 Android 正在抱怨(因为它是一个阻塞操作,所以 UI 将通过抛出 android.os.NetworkOnMainThreadException 来冻结)。如果是这种情况,您可以通过在另一个线程中执行此操作来解决(例如,通过使用 AsyncTask 或外部库,例如 AndroidAsyncHttp)
首先让你的 json 在像 asynchronousTask 这样的网络线程中。
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
然后像这样解析它
try {
JSONObject jObject = new JSONObject(result);
Log.d("username", jObject.getString("Username"));
Toast.makeText(getApplicationContext(), jObject.getString("Username"), Toast.LENGTH_LONG).show();
Log.d("Password", jObject.getString("Password"));
Log.d("eventID", jObject.getString("eventID"));
} catch (JSONException e) {
e.printStackTrace();
}
查看更多信息this
我强烈建议您使用 Volley、Loopj、Okhttp 等网络库进行网络操作。
这是您的问题的代码。
class LongOpreation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String str = "";
try {
str = sendGetRequest();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public String sendGetRequest() throws MalformedURLException {
StringBuilder response = new StringBuilder();
String requrl = "";
requrl = "http://igorsdomain.byethost3.com/getUser.php?username=SailorBoogy&password=qwerty&event=2222";
response = requestExecuter(requrl);
return response.toString();
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
System.out.println("json-----------------------"+jsonObject);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onPreExecute() {
}
public StringBuilder requestExecuter(String str) {
StringBuilder response = new StringBuilder();
try {
URL url = new URL(str);
HttpURLConnection httpconn = (HttpURLConnection) url
.openConnection();
httpconn.setConnectTimeout(5000);
httpconn.setReadTimeout(10000);
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader input = new BufferedReader(
new InputStreamReader(httpconn.getInputStream()));
String strLine = null;
while ((strLine = input.readLine()) != null) {
response.append(strLine);
}
input.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
}
简单调用new LongOperation().execute("");
,你就完成了
我正在尝试将 Json 与 android 一起使用 我有一个 link returns 一个 Json 字符串
InputStream is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
但是,在创建一个新的 JSONObject(jsonText) 时;它压碎了。
这是我的URL:http://igorsdomain.byethost3.com/getUser.php?username=SailorBoogy&password=qwerty&event=2222
这是返回的对象:{"ID":"23","Username":"SailorBoogy","Password":"qwerty","eventID":"6"}
我也试过 JSONObject(jsonText.replaceAll("\"" , "\\\""));
但是没用。
有什么问题吗?我用错了吗?
Json 图书馆:
org.json.JSONException; org.json.JSONObject; org.json.JSONStringer;
没有 LogCat 很难说,但可能你正在 MainThread 上进行网络操作,而 Android 正在抱怨(因为它是一个阻塞操作,所以 UI 将通过抛出 android.os.NetworkOnMainThreadException 来冻结)。如果是这种情况,您可以通过在另一个线程中执行此操作来解决(例如,通过使用 AsyncTask 或外部库,例如 AndroidAsyncHttp)
首先让你的 json 在像 asynchronousTask 这样的网络线程中。
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
然后像这样解析它
try {
JSONObject jObject = new JSONObject(result);
Log.d("username", jObject.getString("Username"));
Toast.makeText(getApplicationContext(), jObject.getString("Username"), Toast.LENGTH_LONG).show();
Log.d("Password", jObject.getString("Password"));
Log.d("eventID", jObject.getString("eventID"));
} catch (JSONException e) {
e.printStackTrace();
}
查看更多信息this
我强烈建议您使用 Volley、Loopj、Okhttp 等网络库进行网络操作。
这是您的问题的代码。
class LongOpreation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String str = "";
try {
str = sendGetRequest();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public String sendGetRequest() throws MalformedURLException {
StringBuilder response = new StringBuilder();
String requrl = "";
requrl = "http://igorsdomain.byethost3.com/getUser.php?username=SailorBoogy&password=qwerty&event=2222";
response = requestExecuter(requrl);
return response.toString();
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
System.out.println("json-----------------------"+jsonObject);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onPreExecute() {
}
public StringBuilder requestExecuter(String str) {
StringBuilder response = new StringBuilder();
try {
URL url = new URL(str);
HttpURLConnection httpconn = (HttpURLConnection) url
.openConnection();
httpconn.setConnectTimeout(5000);
httpconn.setReadTimeout(10000);
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader input = new BufferedReader(
new InputStreamReader(httpconn.getInputStream()));
String strLine = null;
while ((strLine = input.readLine()) != null) {
response.append(strLine);
}
input.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
}
简单调用new LongOperation().execute("");
,你就完成了