Java 截击;将JSONObject请求发布到TextView中的API视图

Java 截击;将JSONObject请求发布到TextView中的API视图,java,android,android-volley,Java,Android,Android Volley,这是我的“MainActivity.java”代码,我无法将JSONObject发布到API服务器,也无法在简单的Textview中获取响应数据。请帮忙。我是Java新手 当我试图构建项目时,它没有显示任何内容 参考链接在上面给出 import android.content.res.Resources; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.

这是我的“MainActivity.java”代码,我无法将JSONObject发布到API服务器,也无法在简单的Textview中获取响应数据。请帮忙。我是Java新手

当我试图构建项目时,它没有显示任何内容

参考链接在上面给出

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private Button getApiBtn, postApiBtn;
    private TextView resultTextView;
    RequestQueue requestQueue;
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resultTextView = (TextView) findViewById(R.id.resultTextView);
        getApiBtn = (Button) findViewById(R.id.getApiBtn);
        postApiBtn = (Button)findViewById(R.id.postApiBtn);

        getApiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });

        postApiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postData();
            }
        });
    }

    // methods are here for post and get
    public void postData() {

        try {
            String url = getResources().getString(R.string.url);
            JSONObject object = new JSONObject();
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    resultTextView.setText("Data Posted");
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("user_Name", "Faruk");
                    params.put("pas_sword", "1234567");
                    return params;
                }
            };
            requestQueue.add(jsonObjectRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getData(){
        try {
            String url = getResources().getString(R.string.url);
            JSONObject object = new JSONObject();
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    resultTextView.setText("Resposne : " + response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                }
            });
            requestQueue.add(jsonObjectRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
导入android.content.res.Resources;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.android.volley.AuthFailureError;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.HashMap;
导入java.util.Map;
公共类MainActivity扩展了AppCompatActivity{
私人按钮getApiBtn,postApiBtn;
私有文本视图结果文本视图;
请求队列请求队列;
私有静态最终字符串标记=MainActivity.class.getSimpleName();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView=(TextView)findViewById(R.id.resultTextView);
getApiBtn=(按钮)findviewbyd(R.id.getApiBtn);
postApiBtn=(按钮)findViewById(R.id.postApiBtn);
getApiBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
getData();
}
});
postApiBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
postData();
}
});
}
//方法在这里发布和获取
public void postData(){
试一试{
字符串url=getResources().getString(R.String.url);
JSONObject对象=新的JSONObject();
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
resultTextView.setText(“数据发布”);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(getApplicationContext(),“Error”,Toast.LENGTH_LONG.show();
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“用户名”、“法鲁克”);
参数put(“帕苏剑”、“1234567”);
返回参数;
}
};
add(jsonObjectRequest);
}捕获(例外e){
e、 printStackTrace();
}
}
public void getData(){
试一试{
字符串url=getResources().getString(R.String.url);
JSONObject对象=新的JSONObject();
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
resultTextView.setText(“Resposne:+response.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(getApplicationContext(),“Error”,Toast.LENGTH_LONG.show();
}
});
add(jsonObjectRequest);
}捕获(例外e){
e、 printStackTrace();
}
}
}

您忘了实例化RequestQueue


为了更清楚地了解这一点,我浏览了你提到的博客。有一件事遗漏了,
AndroidManifest.xml
中的internet权限。请将此
权限添加到您的清单中,然后重试


如果是其他原因导致问题,请清楚地评论您的问题。
postData
方法的密钥名正确吗
pas_

您对此有任何例外吗?您需要在manifest.xml中提供
INTERNET
权限。。你这样做了吗?请分享你的观点。问题到底是什么?对你的声明是的,我同时使用了android.permission.INTERNET和NETWORK\u state。你的代码有问题-你没有初始化
RequestQueue不,你没有。您刚刚声明了一个变量。在
onCreate
方法中添加这行代码
requestQueue=Volley.newRequestQueue(getApplicationContext())我现在还在安卓工作室。在onCreate方法RequestQueue RequestQueue=Volley.newRequestQueue(getApplicationContext())内部声明后;应用程序已停止工作。这与SrtingRequest无关,我想发布JSONObjecti我不是指请求对象。您需要了解java中的对象声明。