我是 android 和凌空的新手。 我创建了一个登录程序来从我的服务器获取 json 数据,但它无法正常工作。 单击登录按钮后,它未显示 json响应。 我正在粘贴下面的代码。
MainActivity.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中
<?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
<?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>
答案 0 :(得分:4)
请将您的jsonRequest更改为StringRequest并向其添加自定义标头:
已编辑:
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;
}
};
感谢。
答案 1 :(得分:2)
而不是以下行:
RequestQueue queue = Volley.newRequestQueue(this);
将其更改为:
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
并在 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);
}
答案 2 :(得分:2)
在android清单行中:
<uses-permission android:name="android.permission.INTERNET"/>'
删除该行的
答案 3 :(得分:1)
当你发送JSON数据时,像这样设置conent类型...... 希望这可能有所帮助
@Override
public Map<String, String> getHeaders ()
throws AuthFailureError {
Map<String, String> params = new HashMap<String,
String>();
params.put("Content-Type", "application/json");
return params;
}
答案 4 :(得分:0)
将以下内容添加到build.gradle文件
编译'com.mcxiaoke.volley:library:1.0.19'
答案 5 :(得分:0)
您也可以执行以下操作:
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) {
}
});
在这里,我们将对象传递给构造函数以获取正确的响应。