简体   繁体   English

Android登录界面 - 已消耗内容

[英]Android Login Interface - Content has been consumed

Hello Everyone I am trying to execute the following code which i saw in a tutorial for my application for login authentication purpose but I am getting the following errors while executing it, Could anyone please help me resolve it? 大家好我试图执行下面的代码,我在我的应用程序的登录认证目的的教程中看到,但我在执行时遇到以下错误,有人可以帮我解决吗? I have gone through other related posts but I either could not find a solution or my programming skills could not interpret it well and solve it 我已经浏览了其他相关帖子,但我要么找不到解决方案,要么我的编程技巧无法解释它并解决它

My Logcat: 我的Logcat:

01-29 01:26:26.997: I/System.out(2250): MessageContent has been consumed
01-29 01:26:26.997: W/System.err(2250): java.lang.IllegalStateException: Content has been consumed
01-29 01:26:26.997: W/System.err(2250):     at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
01-29 01:26:27.007: W/System.err(2250):     at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:100)
01-29 01:26:27.007: W/System.err(2250):     at com.example.kibria.MainActivity.onClick(MainActivity.java:90)
01-29 01:26:27.007: W/System.err(2250):     at android.view.View.performClick(View.java:4438)
01-29 01:26:27.007: W/System.err(2250):     at android.view.View$PerformClick.run(View.java:18422)
01-29 01:26:27.007: W/System.err(2250):     at android.os.Handler.handleCallback(Handler.java:733)
01-29 01:26:27.007: W/System.err(2250):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-29 01:26:27.007: W/System.err(2250):     at android.os.Looper.loop(Looper.java:136)
01-29 01:26:27.017: W/System.err(2250):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-29 01:26:27.017: W/System.err(2250):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 01:26:27.017: W/System.err(2250):     at java.lang.reflect.Method.invoke(Method.java:515)
01-29 01:26:27.017: W/System.err(2250):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-29 01:26:27.017: W/System.err(2250):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-29 01:26:27.017: W/System.err(2250):     at dalvik.system.NativeStart.main(Native Method

My Java class: 我的Java类:

    public class MainActivity extends ActionBarActivity implements OnClickListener {

        EditText etUser, etPass;
        Button bLogin;
        String username, password;
        HttpClient httpclient;
        HttpPost httppost;
        ArrayList<NameValuePair> nameValuePairs;
        HttpResponse response;
        HttpEntity entity;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().build();
            StrictMode.setThreadPolicy(policy);
            setContentView(R.layout.activity_main);
        etUser = (EditText) findViewById(R.id.etUser);
        etPass = (EditText) findViewById(R.id.etPass);
        bLogin = (Button) findViewById(R.id.bSubmit);

        bLogin.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            httpclient = new DefaultHttpClient();

            httppost = new HttpPost("http://10.0.2.2:8080/kibria/index.php");

            username = etUser.getText().toString();
            password = etPass.getText().toString();


            try{

                nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("username", username));
                nameValuePairs.add(new BasicNameValuePair("password", password));



                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpclient.execute(httppost);

                if(response.getStatusLine().getStatusCode()==200){


                    entity = response.getEntity();


                    if(entity.getContent()!=null){


                        InputStream instream = response.getEntity().getContent();

                        JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));


                        String retUser = jsonResponse.getString("user");
                        String retPass = jsonResponse.getString("pass");

                        if(username.equals(retUser)&& password.equals(retPass)){

                            SharedPreferences sp = getSharedPreferences("logindetails", 0);

                            SharedPreferences.Editor spedit = sp.edit();

                            spedit.putString("user", username);
                            spedit.putString("pass", password);

                            spedit.commit();

                            Toast.makeText(getBaseContext(), "SUCCESS!", Toast.LENGTH_SHORT).show();


                        }else {
                            Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show();
                        }
                    }

            }
            }catch (Exception e){
                System.out.println("Message"+e.getMessage());
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "Connection Error!!!", Toast.LENGTH_SHORT).show();
            }


        }

        private static String convertStreamToString(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line = null;
        try{
            while ((line = reader.readLine()) !=null){

                sb.append(line + "\n");
            }
        }   catch(IOException e){
            e.printStackTrace();
        }   finally {
            try{
                is.close();
            }catch (IOException e){

                e.printStackTrace();
            }
        }

    return sb.toString();


    }
}

My PHP file: 我的PHP文件:

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbdb = "mobiledb";

$connect = mysqli_connect($dbhost, $dbuser, $dbpass)
or die("connection error");

mysqli_select_db($connect,$dbdb) or die ("database selection error");

$username = $_POST['username'];
$password = $_POST['password'];

$query = mysqli_query($connect, "SELECT * FROM androidlogin WHERE user = '$username'AND pass = '$password'");

$num = mysqli_num_rows($query);

if($num==1){

while($list=mysqli_fetch_assoc($query)){

$output = $list;
echo json_encode($output);
mysqli_close($connect);
}

}

UPDATED JAVA CODE: 更新的JAVA代码:

public void onClick(View v) {
        // TODO Auto-generated method stub



        httpclient = new DefaultHttpClient();

        httppost = new HttpPost("http://10.0.2.2:8080/kibria/index.php");

        username = etUser.getText().toString();
        password = etPass.getText().toString();


        try{

            nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));



            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response = httpclient.execute(httppost);

            if(response.getStatusLine().getStatusCode()==200){


                //entity = response.getEntity();

                InputStream instream = response.getEntity().getContent();
                if(instream!=null){


                    //InputStream instream = response.getEntity().getContent();

                    JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));


                    String retUser = jsonResponse.getString("user");
                    String retPass = jsonResponse.getString("pass");

                    if(username.equals(retUser)&& password.equals(retPass)){

                        SharedPreferences sp = getSharedPreferences("logindetails", 0);

                        SharedPreferences.Editor spedit = sp.edit();

                        spedit.putString("user", username);
                        spedit.putString("pass", password);

                        spedit.commit();


                        Toast.makeText(getBaseContext(), "SUCCESS!", Toast.LENGTH_SHORT).show();


                    }else {
                        //Toast().execute();
                        Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show();
                    }

                }


        }

        }catch (Exception e){
            System.out.println("Message"+e.getMessage());
            e.printStackTrace();
            Toast.makeText(getBaseContext(), "Connection Error!!!", Toast.LENGTH_SHORT).show();
        }


    }

UPDATED LOGCAT: 更新的LOGCAT:

01-29 02:47:39.722: I/System.out(2081): MessageEnd of input at character 0 of 
01-29 02:47:39.722: W/System.err(2081): org.json.JSONException: End of input at character 0 of 
01-29 02:47:39.732: W/System.err(2081):     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
01-29 02:47:39.732: W/System.err(2081):     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
01-29 02:47:39.742: W/System.err(2081):     at org.json.JSONObject.<init>(JSONObject.java:155)
01-29 02:47:39.742: W/System.err(2081):     at org.json.JSONObject.<init>(JSONObject.java:172)
01-29 02:47:39.742: W/System.err(2081):     at com.example.kibria.MainActivity.onClick(MainActivity.java:99)
01-29 02:47:39.742: W/System.err(2081):     at android.view.View.performClick(View.java:4438)
01-29 02:47:39.742: W/System.err(2081):     at android.view.View$PerformClick.run(View.java:18422)
01-29 02:47:39.742: W/System.err(2081):     at android.os.Handler.handleCallback(Handler.java:733)
01-29 02:47:39.742: W/System.err(2081):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-29 02:47:39.752: W/System.err(2081):     at android.os.Looper.loop(Looper.java:136)
01-29 02:47:39.752: W/System.err(2081):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-29 02:47:39.752: W/System.err(2081):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 02:47:39.752: W/System.err(2081):     at java.lang.reflect.Method.invoke(Method.java:515)
01-29 02:47:39.752: W/System.err(2081):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-29 02:47:39.752: W/System.err(2081):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-29 02:47:39.752: W/System.err(2081):     at dalvik.system.NativeStart.main(Native Method)
You need to take AsynkTask class for server work then it will work fine.

java.lang.IllegalStateException: Content has been consumed java.lang.IllegalStateException:内容已被使用

Because you are calling getContent() method more then one time. 因为你多次调用getContent()方法。

As you see getContent () : 如您所见getContent()

IllegalStateException if this entity is not repeatable and the stream has already been obtained previously IllegalStateException如果此实体不可重复且先前已获取流

Fix it as: 将其修复为:

InputStream instream = response.getEntity().getContent();
if(instream!=null){
  //... your code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在HTTP GET请求之后,结果字符串被截止 - 内容已被消耗 - After HTTP GET request, the resulting string is cut-off - content has been consumed “错误已被消耗”是什么意思? - What does "The error has been consumed" mean? 检查是否已使用java流 - Check if java stream has been consumed 如何检查 InputStream 是否已被使用? - How do I check that an InputStream has been consumed? Java Metro安全性:StreamMessage已被使用 - Java Metro Security: StreamMessage has been already consumed 测试是否已代理Java接口 - Test if a Java Interface has been Proxied 如何避免在android studio上添加android.support.v4.content.Loader.OnLoadCompleteListener - How to avoid android.support.v4.content.Loader.OnLoadCompleteListener has already been added on android studio 你如何断言如果一个异常已经被消耗/处理/捕获,就抛出该异常 - How do you assert that an exception is thrown if it has already been consumed/handled/caught OptionalPendingResult <GoogleSignInResult> 总是引发IllegalStateException,原因是“结果已被使用” - OptionalPendingResult<GoogleSignInResult> is always throwing IllegalStateException with cause “Result has already been consumed” HttpEntity内容已被占用 - HttpEntity content already consumed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM