Java Android:截击请求不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32578407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Volley request is not working
提问by Shivam
I am new to androidand volley. I created one login programto fetch jsondata from my server but it is not working. It is not showing the json responseafter clicking the login button. I am pasting my code below.
我是android和volley 的新手。我创建了一个登录程序来从我的服务器获取json数据,但它不起作用。单击登录按钮后不显示json 响应。我在下面粘贴我的代码。
MainActivity.java
主活动.java
package com.volley.cuser.volleyexample;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.NetworkResponse;
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 com.android.volley.toolbox.Volley;
public class MainActivity extends Activity {
private TextView txtDisplay;
EditText editText;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.username);
editText2 = (EditText) findViewById(R.id.password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void studentLogin(View view) {
String username = editText.getText().toString();
String password = editText2.getText().toString();
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
String url = "http://afterklass.in/api/";
JSONObject js = new JSONObject();
try {
JSONObject jsonobject = new JSONObject();
jsonobject.put("email_mobile", username);
jsonobject.put("passwd", password);
jsonobject.put("m", "student");
jsonobject.put("uc", "signin");
jsonobject.put("signin", "Sign+In");
js.put("data", jsonobject.toString());
}catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, js, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
txtDisplay.setText("Response => " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//String json = null;
//NetworkResponse response = error.networkResponse;
//if(response != null && response.data != null){
//switch(response.statusCode){
//case 400:
//txtDisplay.setText("Error => " + response.data);
//break;
//}
//txtDisplay.setText("Error => " + response.statusCode);
//Additional cases
//}
Log.d("ERROR", error.toString());
}
});
queue.add(jsObjRequest);
}
}
}
activity_main.xml
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username" />
<EditText android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/login"
android:onClick="studentLogin" />
</LinearLayout>
AndroidManifest.xml
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.volley.cuser.volleyexample" >
<uses-permission android:name="android.permission.INTERNET"/>'
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
采纳答案by AndiGeeky
Please change your jsonRequest to StringRequest and add custom header to it :
请将您的 jsonRequest 更改为 StringRequest 并向其添加自定义标头:
Edited :
编辑:
private static final String TAG = YourActivity.class.getSimpleName();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Log.e(TAG, "Successfully signed in : " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error at sign in : " + error.getMessage());
}
}) {
@Override
public HashMap<String, String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("email_mobile", username);
params.put("passwd", password);
params.put("m", "student");
params.put("uc", "signin");
params.put("signin", "Sign+In");
return params;
}
};
Thanks.
谢谢。
回答by Pankaj
Instead of the below line:
而不是以下行:
RequestQueue queue = Volley.newRequestQueue(this);
Change it to like this:
改成这样:
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
And declare you EditTextin onCreatemethod like as follows:
并在onCreate方法中声明您EditText如下:
EditText editText;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.username);
editText2 = (EditText) findViewById(R.id.password);
}
回答by Mahesh B ツ
When you are sending JSON data, set conent type like this... hope this may help
当您发送 JSON 数据时,像这样设置内容类型...希望这可能会有所帮助
@Override
public Map<String, String> getHeaders ()
throws AuthFailureError {
Map<String, String> params = new HashMap<String,
String>();
params.put("Content-Type", "application/json");
return params;
}
回答by Waruna Manjula
add following to your build.gradle file
将以下内容添加到您的 build.gradle 文件中
compile 'com.mcxiaoke.volley:library:1.0.19'
编译'com.mcxiaoke.volley:library:1.0.19'
回答by Rajko
In android manifest line:
在android清单行中:
<uses-permission android:name="android.permission.INTERNET"/>'
remove that ' on end of the line
删除该行末尾的 '
回答by Abhishek Sengupta
You can also do this :
你也可以这样做:
HashMap<String, String> params = new HashMap<String, String>();
params.put("email_mobile", username);
params.put("passwd", password);
JSONObject parameters = new JSONObject(params);
JsonObjectRequest newRequest = new JsonObjectRequest(Request.Method.POST, URL, parametres, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Here we are passing the object to the constructor to get the proper response.
在这里,我们将对象传递给构造函数以获得正确的响应。