问题补充:
我发现用JSON了,换了这个方法还是同样的问题
package jusi.singporecameratest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Test extends Activity {
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "http://appschallenge.juzz4.com/api/login";
HttpPost request = new HttpPost(url);
request.setHeader("Content-Type","application/x-www-form-urlencoded");
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
try{
param.put("username", "demo");
param.put("password", "demo");
param.put("mechanism", "plain");
// 绑定到请求 Entry
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
String retSrc = EntityUtils.toString(httpResponse.getEntity());
// 生成 JSON 对象
JSONObject result = new JSONObject(retSrc);
String token = (String) result.get("reason");
TextView tv = new TextView(this);
tv.setText(token);
setContentView(tv);}
catch(Exception e){
}
}
}