我在我的本地主机中有一个JSON文件,我试图使用排气请求获取我的android应用程序的所有数据。 现在,当我尝试获取我的Parsed数据(使用JSON)时,我收到了一些未知的错误!我正在使用我的模拟器来测试我的应用程序。安卓排气错误,同时获取JSON数据
这里是我的JSON文件:
{"dolaaye_posts":[{"id":"1","post_subject":"salam","post_desc":"salam"},{"id":"2","post_subject":"????","post_desc":"????"}],"success":1}
这里是我的MainActivity.java:
package ir.dolaaye;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.AuthFailureError;
import com.android.volley.Cache;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
public class MainActivity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////////start of Volley Request///////////////
String tag_json_array = "tag_json_array";
/**
* @url = Url for get json Object
*/
String url = "http://10.0.2.2:1378/scripts/dolaaye/posts.json";
/**
* Show Progress Dialog
* Before Get Data From Server
* Or
* Get Error From Server
*/
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("در حال جمع آوری اطلاعات...");
pDialog.show();
/**
* Create Request For Get Json Array From Server
*
* Exist To Method In The Request :
* 1 : onResponse => That Get Response For Server
* If Data Exist In The Server
*
* 2 : onErrorResponse => That Show Error
* If Data Do Not Exist In The Server
*/
JsonArrayRequest jsonArrReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
Log.e("LOG", jsonArray.toString());
for (int i = 0 ; i < jsonArray.length() ; i++)
{
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i("LOG", "object : " + jsonObject.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
}
}
pDialog.hide();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.e("VolleyError", volleyError.getMessage());
if (volleyError instanceof TimeoutError || volleyError instanceof NoConnectionError) {
VolleyLog.e("VolleyError", volleyError.getMessage());
} else if (volleyError instanceof AuthFailureError) {
//TODO
} else if (volleyError instanceof ServerError) {
//TODO
} else if (volleyError instanceof NetworkError) {
//TODO
} else if (volleyError instanceof ParseError) {
//TODO
}
pDialog.hide();
}
});
Cache cache = G.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(url);
if (entry != null){
try {
String data = new String(entry.data, "UTF-8");
Log.i("LOG",data);
pDialog.hide();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
}
G.getInstance().addToRequestQueue(jsonArrReq,tag_json_array);
////end of Volley Request //////////////////////////////////
}
}
我已经将Internet的许可和ACCESS_NETWORK_STATE我的清单。这里是我的logcat:
08-23 13:33:49.767 22388-22388/ir.dolaaye E/Volley: [1] 2.onErrorResponse: VolleyError
我不知道为什么!
你得到的JSONObject响应,并在你的代码中处理JSONArray。 – KishuDroid
在这里检查如何正确地创建一个数组或对象请求 - http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ – Tasos