Je suis nouveau à android et volley. J'ai créé un programme de connexion pour récupérer json données de mon serveur, mais il ne fonctionne pas. Il n'apparaît pas la réponse json après avoir cliqué sur le bouton de connexion . Je colle mon code ci-dessous.Android: demande de Volley ne fonctionne pas
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>
Quel est le message d'erreur ??? – Pankaj
Je ne reçois aucun message d'erreur – Shivam
@Shivam: 'Il ne redirige pas vers l'autre page' où est le code pour démarrer une nouvelle activité? –