java Android:截击请求不起作用
我是新来的安卓和截击。 我创建了一个登录程序,从我的服务器获取json数据,但它不起作用。 单击登录按钮后,它不会显示json响应。 我正在下面粘贴我的代码
主要活动。java
package com.volley.cuser.volleyexample;
import org.json.JSONException;
import org.json.JSONObject;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
import com.安卓.volley.NetworkResponse;
import com.安卓.volley.Request;
import com.安卓.volley.RequestQueue;
import com.安卓.volley.Response;
import com.安卓.volley.VolleyError;
import com.安卓.volley.toolbox.JsonObjectRequest;
import com.安卓.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);
}
}
主要活动。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical">
<EditText 安卓:id="@+id/username"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:hint="@string/username" />
<EditText 安卓:id="@+id/password"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:hint="@string/password" />
<Button
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:text="@string/login"
安卓:onClick="studentLogin" />
</LinearLayout>
AndroidManifest。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.volley.cuser.volleyexample" >
<uses-permission 安卓:name="安卓.permission.INTERNET"/>'
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme" >
<activity
安卓:name=".MainActivity"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
# 1 楼答案
将以下内容添加到您的构建中。格雷德尔锉刀
# 2 楼答案
请将jsonRequest更改为StringRequest,并向其添加自定义标题:
编辑:
谢谢
# 3 楼答案
在android清单行中:
把线的一端去掉
# 4 楼答案
而不是下面的一行:
将其更改为:
并在onCreate方法中声明您EditText,如下所示:
# 5 楼答案
你也可以这样做:
在这里,我们将对象传递给构造函数以获得正确的响应
# 6 楼答案
当你发送JSON数据时,设置conent类型如下。。。 希望这能有所帮助