im 尝试将我的 JSON 对象发送到服务器地址,但它不允许我这样做,在执行方法上给了我一个错误,我已经尝试了这个论坛上关于这个问题的所有答案,但仍然无法让它工作,你认为我的错误是什么?
这是代码
public class MainActivity extends Activity implements OnClickListener{
Button btnLogin, btnRegister;
EditText tvEmail, tvPassword;
TextView tvResultJson1;
Gson g;
AsyncHttpClient client;
Usuario usuario;
public String url = "http://unshakable-kingswood-61-157350.use1-2.nitrousbox.com:9000/login";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlogin);
btnLogin =(Button)findViewById(R.id.btnLogin);
btnRegister=(Button)findViewById(R.id.btnRegister);
tvEmail=(EditText)findViewById(R.id.tvEmail);
tvPassword=(EditText)findViewById(R.id.tvPassword);
tvResultJson1=(TextView)findViewById(R.id.tvResultJson1);
client= new AsyncHttpClient();
g= new Gson();
btnRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Intent i= new Intent(MainActivity.this, RegisterForm.class);
//startActivity(i);
}
});
// check if you are connected or not
if(isConnected()){
}
else{
Toast toast1 =
Toast.makeText(getApplicationContext(),
"there is no internet access", Toast.LENGTH_SHORT);
toast1.show();
finish();
}
btnLogin.setOnClickListener((OnClickListener) this);
}
private boolean isConnected() {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
public void onClick(View view) {
// Get user defined values
sendData();
}
private void sendData() {
String json="";
Usuario usuario = new Usuario();
usuario.setMail(tvEmail.getText().toString());
usuario.setPass(tvPassword.getText().toString());
JsonObject jsonObject= new JsonObject();
jsonObject.addProperty("mail", usuario.getNombre());
jsonObject.addProperty("pass", usuario.getPass());
json = jsonObject.toString();
UploadASyncTask upload = new UploadASyncTask();
upload.execute(jsonObject);
}
private class UploadASyncTask extends AsyncTask<JSONObject, Void, Void>{
@Override
protected Void doInBackground(JSONObject...jsonObject) {
try{
HttpParams params = new BasicHttpParams();
//params.setParameter("data", auth);
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost("http://unshakable-kingswood-61-157350.use1-2.nitrousbox.com:9000/login");
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("data", jsonObject.toString()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
String result = "";
if(inputStream != null){
result="Si funciono";
}
else{
result = "Did not work!";
}
Log.d("RESULT", result);
}catch(Exception e){
Log.e("ERROR IN SEVER UPLOAD", e.getMessage());
}
return null;
}
}
}
你混淆了你的导入。
sendData()
您使用的是JsonObject
- 请注意骆驼案
在您的AsyncTask
中,您使用的是JSONObject
- 注意 JSON 全部大写。