0
嘿傢伙我需要幫助提取JSON值。我試着解析,但結果不出來。JSON幫助HTTPGET返回
繼承人我java代碼:
public class MyAct extends Activity {
EditText etResponse;
TextView tvIsConnected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
String s = getIntent().getStringExtra("username");
// get reference to the views
etResponse = (EditText) findViewById(R.id.etResponse);
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
// check if you are connected or not
if(isConnected()){
tvIsConnected.setBackgroundColor(0xFF00CC00);
tvIsConnected.setText("You are connected");
}
else{
tvIsConnected.setText("You are NOT conncted");
}
// show response on the EditText etResponse
//etResponse.setText(GET("http://hmkcode.com/examples/index.php"));
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute("https://api.github.com/users/" + s + "/repos");
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
String dat = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null) {
result = convertInputStreamToString(inputStream);
dat = result;
}
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
etResponse.setText(result);
}
}
}
輸出我收到這個樣子的:
[
{
"id": 8432681,
"name": "dispersy",
"full_name": "Tribler/dispersy",
"owner": {
"login": "Tribler",
"id": 2913489,
"avatar_url": "",
"gravatar_id": "",
"url": "",
"type": "Organization",
"site_admin": false
},
"private": false,
"size": 8960,
"stargazers_count": 28,
"watchers_count": 28,
"language": "Python",
"has_issues": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 19,
"mirror_url": null,
"open_issues_count": 35,
"forks": 19,
"open_issues": 35,
"watchers": 28,
"default_branch": "devel"
}]
現在我希望提取像HTML_URL
和叉子等,從這個項目。這是JSON
。 幫幫我吧。
一些疑慮。我在哪裏指定我想要提取的字段。 – xyzzyspoon
如何打印輸出? – xyzzyspoon
在onPostExecute() –