读取用户名时出现Android JSON错误
这是我的php脚本,运行良好读取用户名时出现Android JSON错误,android,json,login,Android,Json,Login,这是我的php脚本,运行良好 if (isset($_GET['username']) && isset($_GET['password'])) { $con = mysql_connect("localhost", "aaaa", "bbbb"); if (!$con) { die('Could not connect: '.mysql_error()); } mysql_select_db("login", $con); $u = $_GET['u
if (isset($_GET['username']) && isset($_GET['password'])) {
$con = mysql_connect("localhost", "aaaa", "bbbb");
if (!$con) {
die('Could not connect: '.mysql_error());
}
mysql_select_db("login", $con);
$u = $_GET['username'];
$p = $_GET['password'];
$result = mysql_query("SELECT username FROM users WHERE password = '$p' AND username = '$u' ") or die('Errant query:');
while ($row = mysql_fetch_assoc($result)) {
$output = $row;
}
print(json_encode($output));
mysql_close($con);
} else {
$output = "not found";
print(json_encode($output));
}
这是我的php脚本返回的内容
{"username":"mohamed"}
这是登录活动
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpResponse response = null;
InputStream webs = null;
BufferedReader br = null;
String line = "";
String result = "";
String url = "http://localhost/login.php?username=";
EditText user = (EditText) findViewById(R.id.editText1);
EditText pass = (EditText) findViewById(R.id.editText2);
url = url + user.getText().toString() + "&password=" + pass.getText().toString();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
webs = entity.getContent();
br = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
webs.close();
result = sb.toString();
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
final String res = result;
Button lgn = (Button) findViewById(R.id.button1);
lgn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
try {
JSONObject json = new JSONObject(res);
if (json.getString("username").equals("mohamed")) {
Toast t = Toast.makeText(getApplicationContext(), "logged in", Toast.LENGTH_LONG);
t.show();
} else {
Toast t = Toast.makeText(getApplicationContext(), "Invalid User", Toast.LENGTH_LONG);
t.show();
}
} catch (JSONException e) {
Toast t = Toast.makeText(getApplicationContext(), "Error Reading username", Toast.LENGTH_LONG);
t.show();
e.printStackTrace();
}
}
});
}
}
Toast显示“读取用户名时出错”
有明显的互联网许可
android 4.2.2只需从while循环中删除
\n
,无需编辑此中断行,按原样读取即可
StringBuilder sb=new StringBuilder();
while ((line=br.readLine())!=null) {
sb.append(line);
}
这将使用\n
更改您的json,当您解析此内容时,将抛出异常,因为它不是有效的表单。根据您使用编辑后的回复\n
将作为
{"username":"mohamed"}\n // so here this will invalid json format
这是我如何处理php和json的示例,希望它能帮助您: php: 安卓: web服务部件:
JSONArray jsonArray;
HttpClient httpclient = new DefaultHttpClient();
String url = "http://myurl.com/myphpfile.php";
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = httpclient.execute(getRequest);
final String str = EntityUtils.toString(response.getEntity());
jsonArray = new JSONArray(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
解码json部分:
JSONObject jsonObject;
String id,user,pswd;
try{
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
id = jsonObject.getString("ID");
user = jsonObject.getString("username");
pswd = jsonObject.getString("password");
}
}catch(Exception e){
}
JSONObject-JSONObject;
字符串id、用户、pswd;
试一试{
for(int i=0;i
笔记:
==直接使用web服务将在4.0手机中导致错误,您必须使用异步任务,将web服务部分放在doinbackground部分。
==您可以更改代码以匹配表中的字段、所需的webservices方法(post或get)…从字符串结果创建JSON对象时出错,但我不知道原因:(还有一件事使用AsyncTask在DoinBackground中调用任何webservice请求您解决了吗?
JSONObject jsonObject;
String id,user,pswd;
try{
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
id = jsonObject.getString("ID");
user = jsonObject.getString("username");
pswd = jsonObject.getString("password");
}
}catch(Exception e){
}