【问题标题】:Volley GET & Post JSONObject Request to API View in TextViewVolley GET & Post JSONObject 请求到 TextView 中的 API 视图
【发布时间】:2018-10-25 09:43:25
【问题描述】:

这是我的“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();
        }
    }

}

【问题讨论】:

  • 你有什么例外吗?
  • 你需要在你的 manifest.xml 中提供INTERNET 权限。你这样做了吗?
  • 请分享您的 API
  • 究竟是什么问题?
  • 到你的清单

标签: java android android-volley


【解决方案1】:

你忘了实例化 RequestQueue 。

请参阅此https://developer.android.com/training/volley/simple#java 以获得更清晰的信息

【讨论】:

  • 不是关于 SrtingRequest,我想发布 JSONObject
  • 我不是指请求对象。你需要了解java中的对象声明。
【解决方案2】:

我已经浏览了您提到的博客。缺少一件事是AndroidManifest.xml 的互联网许可。请将此&lt;uses-permission android:name="android.permission.INTERNET"/&gt; 权限添加到您的清单,然后重试。

如果是其他原因导致问题,请清楚地评论您的问题。 postData 方法的键名是否正确? pas_sword

【讨论】:

  • 是的,我同时使用了 android.permission.INTERNET 和 NETWORK_STATE
  • 你的代码有问题 - 你没有初始化RequestQueue requestQueue;
  • 不,你没有。您刚刚声明了一个变量。在您的onCreate 方法中添加这行代码。 requestQueue = Volley.newRequestQueue(getApplicationContext());
  • 我现在还在 Android Studio 中。在 onCreate 方法 RequestQueue 里面声明 requestQueue = Volley.newRequestQueue(getApplicationContext());应用程序停止工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
相关资源
最近更新 更多