POST JsonObject到服务器,android

时间:2014-11-27 22:45:55

标签: android json post request server

我试图将我的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;

        }

    }
        }

1 个答案:

答案 0 :(得分:1)

你混淆了你的进口。

<{1>} sendData()您使用的是JsonObject - 请注意骆驼案例

AsyncTask中你使用的是JSONObject - 注意JSON全都是大写字母。