我正在使用instagram api和
我想阅读Json对象数据
我使用android studio
数据如下:
{"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;
}
}
我从网址获取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"
答案 0 :(得分:0)
**使用排球库进行GET,POST和网络调用,这非常简单有效**
首先创建AppController.java
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
ConnectivityReceiver.connectivityReceiverListener = listener;
}
}
提出请求
private void makeJsonObjectRequest() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
urlJsonObj, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
// Parsing json object response
// response will be a json object
String name = response.getString("access_token");
jsonResponse = "";
jsonResponse += "Name: " + name ;
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
包括排球图书馆
compile 'com.android.volley:volley:1.0.0'
不要忘记在清单文件的应用程序标记中添加
android:name=".AppController"