java - Volley GET 并将 JSONObject 请求发布到 TextView 中的 API View

标签 java android android-volley

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

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

http://www.zoftino.com/get-&-post-data-using-http-library-volley-in-android

上面给出了引用链接。

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();
        }
    }

}

最佳答案

你忘了实例化 RequestQueue 。

看这个https://developer.android.com/training/volley/simple#java为了更清楚

关于java - Volley GET 并将 JSONObject 请求发布到 TextView 中的 API View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52986189/

相关文章:

android kotlin java.io.FileNotFoundException :/storage/emulated/0/number. txt:打开失败:EACCES(权限被拒绝)

java - 如何使用 Volley 获取并等待来自多个 url 的数据

java - 使用 volley 库搜索特定的 json 字符串

Java连接以构建字符串或格式

java - Lucene:如何存储文件内容?

android - 如何通过蓝牙将音频从一台 Android 设备流式传输到另一台 Android 设备?

android - 创建 360 度交互式产品预览

android - 基本网络.performRequest : Unexpected response code 301

java - 开发java(文件)交换格式

java - 将命名参数传递给方法