JSONException:value<;html>;无法将java.lang.string类型的转换为JSONObject
我试图在android中创建一个登录界面,我想连接到一个连接到数据库的php文件。 数据库名为“daymanager”,表名为“login” 这是一个名为login.php的php文件:JSONException:value<;html>;无法将java.lang.string类型的转换为JSONObject,java,php,android,json,Java,Php,Android,Json,我试图在android中创建一个登录界面,我想连接到一个连接到数据库的php文件。 数据库名为“daymanager”,表名为“login” 这是一个名为login.php的php文件: <?php $host="localhost"; $user="root"; $pass=""; $dbname="daymanager"; $con= mysql_connect($host,$user,$pass); $BD= mysql_select_db($dbname, $con); //$
<?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();
}
?>
这是eclipse中的java代码:
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 + "\n");
}
} 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();
}
}
package com.mounzer.test;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.lang.reflect.Constructor;
导入java.lang.reflect.Method;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.conn.scheme.HostNameResolver;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONObject;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.content.SharedReferences;
导入android.database.sqlite.SQLiteDatabase;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类激活扩展活动实现OnClickListener{
按钮登录;
编辑文本用户,通过;
字符串用户名、密码;
HttpClient-HttpClient;
HttpPost-HttpPost;
ArrayList名称值对;
HttpResponse响应;
http实体;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activ);
初始化();
}
私有无效初始化(){
//TODO自动生成的方法存根
login=(按钮)findviewbyd(R.id.login);
user=(EditText)findViewById(R.id.user);
pass=(EditText)findViewById(R.id.pass);
login.setOnClickListener(this);
}
公共void onClick(视图v){
//TODO自动生成的方法存根
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://192.168.48.1:6789/aglprojet/Login.php");
username=user.getText().toString();
password=pass.getText().toString();
尝试
{
namevaluepairs=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,username));
添加(新的BasicNameValuePair(“密码”,password));
setEntity(新的UrlEncodedFormEntity(namevaluepairs));
response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200)
{
entity=response.getEntity();
如果(实体!=null)
{
InputStream instream=entity.getContent();
JSONObject jsonResponse=新的JSONObject(convertStreamToString(instream));
String retUser=jsonResponse.getString(“用户名”);
String retPass=jsonResponse.getString(“密码”);
if(username.equals(retUser)和password.equals(retPass))
{
SharedReferences sp=GetSharedReferences(“登录详细信息”,0);
SharedReferences.Editor spedit=sp.edit();
spedit.putString(“用户”,用户名);
spedit.putString(“pass”,密码);
提交();
Toast.makeText(getBaseContext(),“成功连接”,Toast.LENGTH_SHORT.show();
}else{Toast.makeText(getBaseContext(),“无效用户名或密码”,Toast.LENGTH_SHORT.show();}
}
}
}捕获(例外e)
{
e、 printStackTrace();
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
私有无效日志(字符串、字符串用户){
//TODO自动生成的方法存根
}
私有静态字符串convertStreamToString(InputStream为){
/*
*要将InputStream转换为字符串,我们使用BufferedReader.readLine()命令
*我们迭代直到BufferedReader返回null,这意味着
*没有更多的数据可读取。每行都将附加到StringBuilder
*并作为字符串返回。
*/
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(IOE异常){
e、 printStackTrace();
Toast.makeText(null,例如toString(),Toast.LENGTH_SHORT).show();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
Toast.makeText(null,例如toString(),Toast.LENGTH_SHORT).show();
}
}
使某人返回字符串();
}
}
当我在手机上尝试应用程序时,会出现以下异常:
org.json.JSONException:类型为java.lang.string的值
无法转换为JSONObject
谁能告诉我该怎么办
谢谢我猜是有什么东西出现了错误并返回了错误消息