dpd46554 2012-12-31 12:45
浏览 227

JSONException:java.lang.string类型的值<html>无法转换为JSONObject

I'm trying to make a login interface in android , i want to connect to a php file that connects to a database. the database is called "daymanager" and the table is "login"

And this is the php file called login.php :

<?php

$host="localhost";
$user="root";
$pass="";
$dbname="daymanager";

$con= mysql_connect($host,$user,$pass);
$BD= mysql_select_db($dbname, $con);
//$username=$_POST['username'];
//$password=$_POST['password'];

$query=mysql_query("select username,password from login");
$num=mysql_num_rows($query);

if($num==1)
{
while($list=mysql_fetch_array($query))
{
$output=$row[username];
echo json_encode($output);

}
mysql_close();
}
?>

and this is the java code in eclipse :

package com.mounzer.test;





import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.HostNameResolver;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Activating extends Activity implements OnClickListener {
    Button login;
    EditText user,pass;
    String username,password;

    HttpClient httpclient;
    HttpPost httppost;
    ArrayList<NameValuePair> namevaluepairs;
    HttpResponse response;
    HttpEntity entity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub







        super.onCreate(savedInstanceState);
        setContentView(R.layout.activ);



        initialise();
    }

    private void initialise() {
        // TODO Auto-generated method stub
        login =(Button) findViewById(R.id.login);
        user=(EditText) findViewById(R.id.user);
        pass=(EditText) findViewById(R.id.pass);
        login.setOnClickListener(this);
    }

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




        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        httpclient = new DefaultHttpClient();

        httppost =new HttpPost("http://192.168.48.1:6789/aglprojet/Login.php");


        username=user.getText().toString();
        password=pass.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 !=null)
                {
                    InputStream instream=entity.getContent();
                    JSONObject jsonResponse=new JSONObject(convertStreamToString(instream));

                    String retUser = jsonResponse.getString("username");
                    String retPass = jsonResponse.getString("password");



                    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(),"Succesfully connected", Toast.LENGTH_SHORT).show();

                    }else {Toast.makeText(getBaseContext(),"Invalid username or password", Toast.LENGTH_SHORT).show(); }
                }


            }


        }catch(Exception e)

        {
            e.printStackTrace();
            Toast.makeText(getBaseContext(),e.toString(), Toast.LENGTH_LONG).show();
        }



    }
     private void Log(String string, String retUser) {
        // TODO Auto-generated method stub

    }

    private static String convertStreamToString(InputStream is) {
            /*
             * To convert the InputStream to String we use the BufferedReader.readLine()
             * method. We iterate until the BufferedReader return null which means
             * there's no more data to read. Each line will appended to a StringBuilder
             * and returned as String.
             */
          BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "
");
                }
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(null, e.toString(), Toast.LENGTH_SHORT).show();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(null, e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
            return sb.toString();
        }

}

when I try the application from my mobile , it gives me this exception :

org.json.JSONException : value <html> of type java.lang.string cannot be converted to JSONObject

can anyone tell me what should I do please thank you

  • 写回答

1条回答 默认 最新

  • douxianxing5712 2012-12-31 12:52
    关注

    My guess is that something is running into an error and returning an error message wrapped in HTML. Try calling your authentication script via a browser or anything that lets you see what it actually returns.

    Also, you don't seem to return any fields in your output, so getString("username") etc is never going to work. If you want to return multiple things from your authentication script, put them into an array or something and json_encode that.

    And finally, are you sure it's a good idea to send the password to your client...? (I'm guessing you're trying to do this judging by the fact that you try to read them back from the response)

    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)