【发布时间】:2017-08-09 10:43:49
【问题描述】:
我正在使用 instagram api 和
我要读取Json对象数据
我正在使用安卓工作室
数据是这样的:
{"access_token": "1505236317.d0d63a5.7c51f076228e4b0fa40e2ed83666f709",
"user": {"id": "1505236317", "username": "arash_s.t", "profile_picture":
"https://scontent.cdninstagram.com/t51.2885-
19/s150x150/18878865_233940920435631_1005661172109672448_a.jpg", "full_name":
"Arash", "bio": "\u13d8R\u13d8\u13a6H_\u13a6T. DONT CHOSE YOUR LIFE AS OTHERS
.BUT CREAT IT WITHOUT LIMITATIONS\ud83c\udf0a", "website": ""}}
我正在使用这个类
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject 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 JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
我正在从 url 获取 json 对象---->
url = "https://api.instagram.com/oauth/access_token";
new JSONParse().execute();
我在下面代码中的这一行有问题
kelidestan = json.getJSONObject(json_name);
但是当我运行我的应用程序时 kelidetan 为空
public class JSONParse extends AsyncTask<String, String, JSONObject> {
public ProgressDialog pDialog;
@Override
public void onPreExecute() {
super.onPreExecute();
/*pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();*/
}
@Override
public JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
public void onPostExecute(JSONObject json) {
//pDialog.dismiss();
try {
// kelidestan
kelidestan = json.getJSONObject(json_name);
// build String
final int len=kelidestan.length();
final ProgressDialog p2Dialog = new ProgressDialog(Login.this);
Login.this.runOnUiThread(new Runnable() {
public void run() {
p2Dialog.setMessage(len+"lolo");
p2Dialog.setIndeterminate(false);
p2Dialog.setCancelable(true);
p2Dialog.show();
}
});
} catch (final JSONException e) {
Login.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(Login.this, (CharSequence) e.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
问题是:“如何从php网站读取json对象数据?”
用户名等:"username": "arash_s.t"
【问题讨论】:
-
json_decode?无法真正理解您的问题。
-
你为什么标记 PHP?
-
为什么会有 PHP 标签?
标签: java android json instagram-api