我是一名新手,想学习关于Android和Volley的知识。
我创建了一个登录程序,用于从我的服务器获取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>
它没有重定向到其他页面
,启动新Activity的代码在哪里? - ρяσѕρєя K