我正在尝试使用php脚本从android中获取MySQL中的数据,我已经使用postman测试了php脚本,并且它回复了数据,但我的android编码看起来不太好我在logcat中都没有收到任何错误文件。这是代码
public class personal_information extends Fragment {
private TextView textViewResult;
private ProgressDialog loading;
private SQLiteHandler db;
private String uniqueId;
private String phone;
private String email;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.personal_information,container,false);
textViewResult = (TextView) view.findViewById(R.id.personal_info);
db = SQLiteHandler.getInstance(getContext());
//Fetching uniqueID
HashMap<String,String> user = db.getUserDetails();
uniqueId = user.get("uid");
phone = user.get("phone");
email = user.get("email");
displayData();
//Return the layout file after inflating
return view;
}
private void displayData(){
String stringRetrieve = "Retrieve values";
loading = ProgressDialog.show(getContext(),"Loading data ...","",false,false);
loading.setCancelable(true);
String url = AppConfig.URL_DisplayPersonalInformation+uniqueId;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
//Adding request to queue.
AppController.getInstance().addToRequestQueue(stringRequest,stringRetrieve);
}
private void showJSON(String response) {
String name = "";
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(AppConfig.JSON_ARRAY);
JSONObject personalData = result.getJSONObject(0);
name = personalData.getString(AppConfig.first_name)+" "+personalData.getString(AppConfig.last_name);
}catch (JSONException e){
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name+"\nPhone:\t" +phone+ "\nEmail:\t"+ email);
}
}
这是我正在尝试建立请求队列的AppController文件。
public class AppController extends MultiDexApplication {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
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);
}
}
}
你能帮助我吗?
谢谢。
答案 0 :(得分:0)
所以,你正在使用Volley网络库。
首先,您需要setup a request queue。
启动请求队列: mRequestQueue.start()
。
创建您的请求。你实际上已经创造了它。但是为什么不使用JsonObjectRequest
,无论如何你都要把它解析为JSON。虽然也可以使用StringRequest
,但JsonObjectRequest
可供您使用。
JsonObjectRequest request = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// do something with response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// handle error
}
});
将其添加到请求队列:addToRequestQueue(...)
。
其他提示:
personal_information
这样的类名,使用像PersonalInformation
这样的驼峰案例是惯例,或者PersonalInformationFragment
更好,因为它是一个片段。