0

我有一个 android 移动应用程序,它将使用一个名为 " login.php" 的 php 文件连接到包含用户名和密码的数据库,php 文件的结果是JSON格式,这就是它{"username":"mounzer","password":"yaghi"} 的外观:至于移动应用程序,这是代码 :

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();
    // defaultHttpClient
    httpclient = new DefaultHttpClient();
    httppost = new HttpPost("http://192.168.1.38/mobileappd/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",username));

        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_LONG).show();
                }
            }
        }
    } catch(Exception e) {  
        e.printStackTrace();
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
    }
}

好吧,它给了我以下错误:“ org.json.JSONException:value DOCTYPE of type java.lang.string cannot be converted to JSONobject”有人可以告诉我代码中的错误在哪里。

4

2 回答 2

0

这里错误消息说

Value `<!DOCTYPE` of type java.lang.String cannot be converted to JSONObject

如果是<!DOCTYPE那么不是{"username":….

您需要准确了解正在发出的请求以及服务器返回 HTML 文档的原因。

检查您的服务器日志。它可能是一个错误文档。

于 2013-01-03T18:15:06.880 回答
0

检查从您正在调用的链接返回的结果,我认为它正在返回 html 页面,因为它说“值 DOCTYPE 无法转换为 JSONOBject”。因此,请检查您通过调用该链接得到的响应

于 2013-01-03T18:15:36.107 回答