Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图在用户单击android中的特定选项卡时自动获取数据_Java_Android - Fatal编程技术网

Java 试图在用户单击android中的特定选项卡时自动获取数据

Java 试图在用户单击android中的特定选项卡时自动获取数据,java,android,Java,Android,我正在尝试使用php脚本从android中的MySQL获取数据,我已经使用postman测试了php脚本,它用数据返回,但是我的android编码似乎不太好,我在logcat文件中也没有得到任何错误。下面是代码 public class personal_information extends Fragment { private TextView textViewResult; private ProgressDialog loading; private SQLite

我正在尝试使用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);
    }
}
公共类个人信息扩展片段{
私有文本视图文本视图结果;
私有对话框加载;
私有SQLiteHandler数据库;
私有字符串唯一性;
私人电话;
私人字符串电子邮件;
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(右布局。个人信息,容器,错误);
textViewResult=(TextView)view.findViewById(R.id.personal_info);
db=SQLiteHandler.getInstance(getContext());
//获取唯一的
HashMap user=db.getUserDetails();
uniqueId=user.get(“uid”);
phone=user.get(“phone”);
email=user.get(“email”);
显示数据();
//充气后返回布局文件
返回视图;
}
私有void displayData(){
String stringRetrieve=“检索值”;
loading=ProgressDialog.show(getContext(),“正在加载数据…”,“”,false,false);
loading.setCancelable(真);
字符串url=AppConfig.url\u DisplayPersonalInformation+uniqueId;
StringRequest StringRequest=新的StringRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
loading.dispose();
showJSON(响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
//正在将请求添加到队列。
AppController.getInstance().addToRequestQueue(stringRequest,stringRetrieve);
}
私有void showJSON(字符串响应){
字符串名称=”;
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray result=jsonObject.getJSONArray(AppConfig.JSON_数组);
JSONObject personalData=result.getJSONObject(0);
name=personalData.getString(AppConfig.first\u name)+“”+personalData.getString(AppConfig.last\u name);
}捕获(JSONException e){
e、 printStackTrace();
}
textViewResult.setText(“名称:\t”+名称+”\n电话:\t”+电话+“\n邮件:\t”+电子邮件);
}
}
这是我试图从中创建请求队列的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);
        }
    }
}
公共类AppController扩展了多索引应用程序{
公共静态最终字符串标记=AppController.class.getSimpleName();
私有请求队列mRequestQueue;
专用静态应用控制器;
@凌驾
public void onCreate(){
super.onCreate();
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
mInstance=这个;
}
公共静态同步AppController getInstance(){
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
}
返回mrequest队列;
}
公共无效addToRequestQueue(请求请求,字符串标记){
请求setTag(TextUtils.isEmpty(tag)?tag:tag;
getRequestQueue().add(请求);
}
公共无效addToRequestQueue(请求请求){
要求设置标签(标签);
getRequestQueue().add(请求);
}
公共作废取消挂起请求(对象标记){
if(mRequestQueue!=null){
mRequestQueue.cancelAll(标记);
}
}
}
你能帮我吗?

谢谢。

那么,您正在使用Volley网络库

  • 首先,你需要

  • 启动请求队列:
    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
        }
    });
    
    JsonObjectRequest=newjsonobjectrequest
    (Request.Method.GET,url,null,new Response.Listener()){
    @凌驾
    公共void onResponse(JSONObject响应){
    //做些有反应的事情
    }
    },new Response.ErrorListener(){
    @凌驾
    公共无效onErrorResponse(截击错误){
    //处理错误
    }
    });
    
  • 将其添加到请求队列:
    addToRequestQueue(…)

  • 其他提示:

    • 不要像
      personal\u information
      那样使用类名,通常会像
      PersonalInformation
      那样使用驼峰大小写,或者
      PersonalInformationFragment
      更好,因为它是一个片段
    • 不要在主线程上执行数据库查询(无论如何,这不属于本问题的主题)

    您正在使用截击库吗?创建请求后,您需要调用
    addToRequestQueue
    。是的,我正在使用volley库。首先,您需要,然后
    addToRequestQueue
    谢谢,我知道了,我只是忘了调用addToRequestQueue。我已经编写了一个类文件来设置请求队列。@BornToCode我无法修复它,我已经更新了我的代码,你能检查一下吗?谢谢!!将检查它,但您能告诉我何时使用JSONObjectRequest和StringRequest吗?@luke use
    StringRequest
    对于一般需要,即您需要原始字符串,如果您希望凌空将其直接解析为JSONObject,请使用
    JSONObjectRequest