Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓:截击请求不起作用_Java_Android_Http_Android Volley - Fatal编程技术网

Java 安卓:截击请求不起作用

Java 安卓:截击请求不起作用,java,android,http,android-volley,Java,Android,Http,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.Bundl

我是新来的安卓截击。 我创建了一个登录程序,从我的服务器获取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);
    }
package com.volley.cuser.volleyexample;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.TextView;
导入com.android.volley.NetworkResponse;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
公共类MainActivity扩展了活动{
私有文本视图txtDisplay;
编辑文本编辑文本;
编辑文本编辑文本2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(editText)findViewById(R.id.username);
editText2=(EditText)findViewById(R.id.password);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
public void studentLogin(视图){
字符串username=editText.getText().toString();
字符串密码=editText2.getText().toString();
RequestQueue=Volley.newRequestQueue(MainActivity.this);
字符串url=”http://afterklass.in/api/";
JSONObject js=新的JSONObject();
试一试{
JSONObject JSONObject=新的JSONObject();
jsonobject.put(“email\u mobile”,用户名);
jsonobject.put(“passwd”,password);
jsonobject.put(“m”,“学生”);
jsonobject.put(“uc”、“signin”);
jsonobject.put(“Sign”、“Sign+In”);
put(“data”,jsonobject.toString());
}捕获(JSONException e){
e、 printStackTrace();
}
JsonObjectRequest jsObjRequest=新的JsonObjectRequest(Request.Method.POST,url,js,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
//TODO自动生成的方法存根
txtDisplay.setText(“Response=>”+Response.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//字符串json=null;
//NetworkResponse=错误。NetworkResponse;
//if(response!=null&&response.data!=null){
//开关(响应状态代码){
//案例400:
//txtDisplay.setText(“Error=>”+response.data);
//中断;
//}
//txtDisplay.setText(“Error=>”+response.statusCode);
//其他案例
//}
Log.d(“ERROR”,ERROR.toString());
}
});
add(jsObjRequest);
}
}

活动\u 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>
<?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>

AndroidManifest.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>
<?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>

'

而不是下面的行:

 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);
  }

当您发送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;
    }
@覆盖
公共映射getHeaders()
抛出AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/json”);
返回参数;
}

请将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;
    }
};
private static final String TAG=YourActivity.class.getSimpleName();
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//显示响应字符串的前500个字符。
Log.e(标记“成功登录:”+response.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(标记“登录时出错:”+Error.getMessage());
}
}) {
@凌驾
公共HashMap getParams(){
HashMap params=新的HashMap();
参数put(“email_mobile”,用户名);
参数put(“passwd”,password);
参数put(“m”、“学生”);
参数put(“uc”、“SIGN”);
参数put(“登录”、“登录”);
返回参数;
}
};

谢谢。

将以下内容添加到build.gradle文件中

编译'com.mcxiaoke.volley:library:1.0.19'


在android清单行中:

<uses-permission android:name="android.permission.INTERNET"/>'
'
删除该行末尾的“

您也可以这样做:

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;
    }
};
HashMap params=newhashmap();
参数put(“email_mobile”,用户名);
参数put(“passwd”,password);
JSONObject参数=新的JSONObject(参数);
JsonObjectRequest newRequest=新JsonObjectRequest(Request.Method.POST,URL,参数,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){