使用 HttpURLConnection 请求 JSON 文档并解析它
requesting JSON document with HttpURLConnection and parsing it
我有一个带有 class 的应用程序,它负责从本地服务器请求 JSON...
这是主要内容 Activity:
public class Activity_main extends AppCompatActivity {
private ArrayList<StructAcount> netAcount = new ArrayList<StructAcount>();
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.txtJSON);
WebService.readUrl("http://localhost/match_picture/service.php?action=read");
String result = WebService.getResult();
if (result != null) {
try {
JSONArray tasks = new JSONArray(result);
for (int i=0; i<tasks.length(); i++) {
StructAcount acount= new StructAcount();
JSONObject object = tasks.getJSONObject(i);
acount.id = object.getLong("user_id");
acount.name = object.getString("user_name");
acount.email = object.getString("user_email");
netAcount.add(acount);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(Activity_main.this, "hsh" , Toast.LENGTH_SHORT).show();
}
for (StructAcount acount: netAcount) {
textView.setText(textView.getText() + "username: " + acount.name + " | " + "useremail: " + acount.email + "\n");
Toast.makeText(Activity_main.this, "username: " + acount.name + "\n" + "useremail: " + acount.email , Toast.LENGTH_SHORT).show();
}
}
}
...这里是 HTTP 请求的 class:
public class WebService {
private static String result;
public static void readUrl(String server_url) {
class GetJson extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
BufferedReader bufferedReader = null;
String uri = params[0];
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
InputStream in = new BufferedInputStream(con.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(in));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json);
}
return sb.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
WebService.set_result(s);
}
}
GetJson gj = new GetJson();
gj.execute(server_url);
}
public static void set_result(String s) {
result = s;
}
public static String getResult() {
return result;
}
}
我第一次 运行 应用程序,没有为 TextView 设置任何内容。
当应用程序仍在模拟器的屏幕上时:如果我做了一个小改动,例如,更改 Toast 消息的文本,我会再次 运行 应用程序并由 [=28= 设置 TextView 的文本] 值 ...
任何人都可以针对这种情况提供一些参考或解决方案吗?
出于学习目的,HttpUrlConnection
可能会有用。
对于现实生活,Google 推荐使用 Android Volley 库。
一些有用的链接:
并且为了使用 Android 带 HTTPS 的 Volley,不需要核对 SSL 证书(请不要)。
为什么?答案是here.
我有一个带有 class 的应用程序,它负责从本地服务器请求 JSON...
这是主要内容 Activity:
public class Activity_main extends AppCompatActivity {
private ArrayList<StructAcount> netAcount = new ArrayList<StructAcount>();
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.txtJSON);
WebService.readUrl("http://localhost/match_picture/service.php?action=read");
String result = WebService.getResult();
if (result != null) {
try {
JSONArray tasks = new JSONArray(result);
for (int i=0; i<tasks.length(); i++) {
StructAcount acount= new StructAcount();
JSONObject object = tasks.getJSONObject(i);
acount.id = object.getLong("user_id");
acount.name = object.getString("user_name");
acount.email = object.getString("user_email");
netAcount.add(acount);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(Activity_main.this, "hsh" , Toast.LENGTH_SHORT).show();
}
for (StructAcount acount: netAcount) {
textView.setText(textView.getText() + "username: " + acount.name + " | " + "useremail: " + acount.email + "\n");
Toast.makeText(Activity_main.this, "username: " + acount.name + "\n" + "useremail: " + acount.email , Toast.LENGTH_SHORT).show();
}
}
}
...这里是 HTTP 请求的 class:
public class WebService {
private static String result;
public static void readUrl(String server_url) {
class GetJson extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
BufferedReader bufferedReader = null;
String uri = params[0];
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
InputStream in = new BufferedInputStream(con.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(in));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json);
}
return sb.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
WebService.set_result(s);
}
}
GetJson gj = new GetJson();
gj.execute(server_url);
}
public static void set_result(String s) {
result = s;
}
public static String getResult() {
return result;
}
}
我第一次 运行 应用程序,没有为 TextView 设置任何内容。
当应用程序仍在模拟器的屏幕上时:如果我做了一个小改动,例如,更改 Toast 消息的文本,我会再次 运行 应用程序并由 [=28= 设置 TextView 的文本] 值 ...
任何人都可以针对这种情况提供一些参考或解决方案吗?
出于学习目的,HttpUrlConnection
可能会有用。
对于现实生活,Google 推荐使用 Android Volley 库。
一些有用的链接:
并且为了使用 Android 带 HTTPS 的 Volley,不需要核对 SSL 证书(请不要)。
为什么?答案是here.