1
我想发送一个带有信息电子邮件地址,名字和姓氏的JSON,并期望来自服务器的响应表示{status:“Created”}或{status:“Resend”}并根据答案会有一个弹出消息。我想知道我用什么从状态类中提取信息。谢谢! 这里是我的代码接受接收JSON响应(android)
protected void sendJson(final String email, final String firstN,
final String lastN) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); // For Preparing Message Pool for the child
// Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),
10000); // Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
// post in the url
HttpPost post = new HttpPost(
"https://iphone-radar.com/accounts");
json.put("email_address", email);
json.put("first_name", firstN);
json.put("last_name", lastN);
StringEntity se = new StringEntity("JSON: "
+ json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(se);
response = client.execute(post);
/* Checking response */
if (response != null) {
String str = response.getEntity().toString();
if (str.equals("Created")) {
new AlertDialog.Builder(CreateAccount.this)
.setTitle("Account Creation Successful")
.setMessage(
"An activation code has been sent to you. Please check your SPAM folder if you do not receive your activation code email")
.setNeutralButton("OK", null).show();
} else if(str.equals("Resend")) {
new AlertDialog.Builder(CreateAccount.this)
.setTitle("Code Resent")
.setMessage(
"Your activation code has been resent to your email.\n\nIf you are not receiving your activation code, our email is being blocked. Please email us at '[email protected]' and we will manually send you a code.")
.setNeutralButton("OK", null).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
谢谢你的提示,但是当我把它通过调试线路org.json.JSONObject的obj =新org.json.JSONObject(org.apache.http.util.EntityUtils.toString( response.getEntity()));发送给我(异常e)我或服务器有问题吗?再次感谢 – Sean
它给了什么例外?使用'android.util.Log.v(“EXCEPTION”,“[”+ e.getMessage()+“]”,e);'在日志中打印出整个异常。要么是响应的实体为null(可能是服务器),要么在EntityUtils.toString方法中存在一些转换错误。 – Femi
它说[值为的java.lang.String类型不能转换为JSONObject]这是否意味着我使用了错误的网站?再次感谢 – Sean