【发布时间】:2017-06-07 04:23:26
【问题描述】:
嘿,我想将 JSON 字符串发送到服务器并在 JSON 数组中获取响应,
请帮我弄清楚用什么来发送 json 数组并以 json 格式返回响应,我的代码是
public class Welcome extends AppCompatActivity {
private static final String LOGIN_REQUEST_URL = "http://demo4u.org/leaveapp/ws/login.php";
JSONObject request = new JSONObject();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
final EditText user_email = (EditText) findViewById(R.id.username);
final EditText user_password = (EditText) findViewById(R.id.password);
final Button signin = (Button) findViewById(R.id.signin);
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String uname = user_email.getText().toString();
final String pass = user_password.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
try {
JSONObject jsonresponse = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
try {
request.put("user_email", uname);
} catch (JSONException e) {
e.printStackTrace();
}
try {
request.put("user_password", pass);
} catch (JSONException e) {
e.printStackTrace();
}
final TextView show = (TextView) findViewById(R.id.res);
String jsonstring = request.toString();
JsonObjectRequest jsonobjreq = new JsonObjectRequest(
Method.POST, LOGIN_REQUEST_URL, jsonstring,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Log.d(Tag, toString());
Toast.makeText(Welcome.this,"Volley Error",Toast.LENGTH_LONG).show();
show.setText(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(Welcome.this,"Volley Error",Toast.LENGTH_LONG).show();
VolleyLog.d(Tag, "Error : " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
} catch (JSONException e) {
}
请写出我必须用上面的代码替换的代码。
【问题讨论】:
-
这里有什么异常吗?请发布堆栈跟踪
-
你能把你的邮件发给我,这样我就可以转发我的代码,因为我现在真的很沮丧……或者通过 yashkumaratri@gmail.com 联系我
标签: java android json android-studio