android - 在 Android 中登录的空 JSON 响应

标签 android json

我正在使用 android 中的服务器进行注册和登录。注册成功完成,但登录返回空响应。

我的登录页面如下,

package com.example.arunraj.locationtracker;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class MainActivity extends ActionBarActivity {
    String L_URL= "http://safetracker-threetinker.rhcloud.com/api/login";
    EditText e_user,e_pwd;
    String c_username,c_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        e_user = (EditText) findViewById(R.id.e_user);
        e_pwd = (EditText) findViewById(R.id.e_pwd);
        Button b_login = (Button) findViewById(R.id.b_login);
        Button b_register = (Button) findViewById(R.id.b_register);
        b_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                c_username=e_user.getText().toString();
                c_password=e_pwd.getText().toString();
                LoginAsyn loginAsyn=new LoginAsyn();
                loginAsyn.execute(L_URL);
//
            }
        });
        b_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it i present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    class LoginAsyn extends AsyncTask<String,Void,String>
    {
        private ProgressDialog nDialog;






        @Override
        protected void onPreExecute()
        {

            //progress dialog
            nDialog = new ProgressDialog(MainActivity.this);
            nDialog.setMessage("Loading..");
            nDialog.setTitle("Login....");
            nDialog.setIndeterminate(false);
            nDialog.setCancelable(true);
            nDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            InputStream inputStream = null;
            String result = "";
            try {
                // 1. create HttpClient
                HttpClient httpclient = new DefaultHttpClient();
                // 2. make POST request to the given URL
                HttpPost httpPost = new HttpPost(params[0]);
                String json = "";
                // 3. build jsonObject
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", c_username);
                jsonObject.put("password", c_password);


                // 4. convert JSONObject to JSON to String
                json = jsonObject.toString();

                // 5. set json to StringEntity
                StringEntity se = new StringEntity(json);
                // 6. set httpPost Entity
                httpPost.setEntity(se);
                // 7. Set some headers to inform server about the type of the content   
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");
                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPost);







                inputStream = httpResponse.getEntity().getContent();


                BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
                String line = "";
                while((line = bufferedReader.readLine()) != null)
                    result += line;

                inputStream.close();



            } catch (Exception e) {
            }

            // 11. return result
            return result;

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (nDialog.isShowing()) {
                nDialog.dismiss();
            }



            Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();


        }

    }



}

预期的json输入如下,

{
   "name":"hima",
   "password":"sample123"
}

和响应,

{
  "userId": 1,
  "userName": "hima"
}

但是我得到了 org.apache.http.client.ClientProtocolException 的空白响应。我需要帮助才能找到问题所在。 下面给出日志猫

07-30 12:41:49.713 31015-31015/? V/GAV4﹕Thread[main,5,main]:连接到服务 07-30 12:41:49.719 31015-31033/? I/GAV4:Thread[GAThread,5,main]:未找到 Activity 数据。 07-30 12:41:49.719 31015-31033/? V/GAV4﹕Thread[GAThread,5,main]:初始化的GA线程 07-30 12:41:49.878 744-813/? I/ActivityManager:启动proc 31097:com.android.chrome:privileged_process1/u0a34 for service com.android.chrome/org.chromium.content.app.PrivilegedProcessService1 07-30 12:41:49.921 31097-31097/? I/ChildProcessService:创建新的 ChildProcessService pid=31097 07-30 12:41:49.924 744-2781/? I/ActivityManager:杀死 27220:com.google.android.apps.photos/u0a65 (adj 15): empty #17 07-30 12:41:50.053 744-1192/? I/ActivityManager:杀死 30231:com.google.android.apps.gcs/u0a84 (adj 15): empty #17 07-30 12:41:50.982 744-764/? V/WindowManager:不是 Base app: 添加窗口 Window{161d5b01 u0 Login....} at 6 of 21 07-30 12:41:51.853 30701-31115/com.example.arunraj.locationtracker I/System.out:异常nnnnnnnnnnnnnnnnnnnnnnnnorg.apache.http.client.ClientProtocolException 07-30 12:41:51.881 744-897/? W/InputMethodManagerService:窗口已经聚焦,忽略焦点增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@8173b94 attribute=null, token = android.os.BinderProxy@3dcb4c75

最佳答案

您的 URL ="http://safetracker-threetinker.rhcloud.com/api/login"无效。首先检查一下。

关于android - 在 Android 中登录的空 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31704648/

相关文章:

ios - 无法向数组添加元素

android - 从哪里获得这个丢失的文件 - Android Studio,模拟器

android - 具有 ConstraintLayout 对话框宽度的 AppCompatDialogFragment

android - 不使用 ContentProvider 时使用 CursorLoader

php - 从 PHP JSON 返回 SQL Blob

Android post 请求(JSON 参数)到 WCF restful WS

json - 访问解码为 HASH 和哈希引用数组的 JSON

ios - 如何通过 Post 将对象列表动态发送到我们的服务器

android - 在 IntentService 中执行多个查询

php - 如何获得更多的变量值?