[英]connection to server database in Android
I'm trying to connect my Android app to server but I get errors and I don't know what is wrong. 我正在尝试将我的Android应用程序连接到服务器,但出现错误,我不知道出了什么问题。
Here is my MainActivity class: 这是我的MainActivity类:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
JsonReader jsonReader=new JsonReader(new JsonReader.AsyncResponse() {
@Override
public void processfinish(JSONObject output) {
Gson gson=new Gson();
String message=gson.toJson(output);
Toast.makeText(getApplicationContext(),"this is "+message,Toast.LENGTH_LONG).show();
}
});
jsonReader.execute();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}
and this is my JsonReader class: 这是我的JsonReader类:
public class JsonReader extends AsyncTask<String,String,JSONObject>{
final static String myurl="http://example.com";
public interface AsyncResponse{
void processfinish(JSONObject output);
}
public AsyncResponse delegate = null;
public JsonReader(AsyncResponse delegate){
this.delegate = delegate;
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
}
finally {
is.close();
}
}
@Override
protected JSONObject doInBackground(String... strings) {
try {
JSONObject s = readJsonFromUrl(myurl);
return s;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
delegate.processfinish(jsonObject);
}
}
I created AsyncResponse using this guide: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? 我使用以下指南创建了AsyncResponse: 由于AsyncTask是一个单独的类,如何将OnPostExecute()的结果获取到主要活动中?
And my PHP server code: 而我的PHP服务器代码:
<?php
$dbhost = "myhost";
$dbuser = "myuser";
$dbname = "myname";
$dbpass = "mypassword";
$connect_db = mysql_connect ($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname,$connect_db);
if ($connect_db){
$result= mysql_query("SELECT * FROM `ask` ",$connect_db)or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$qa=new StdClass();
$qa->Question=$row['Question'];
$qa->Answer=$row['Answer'];
$qa->Answer2=$row['Answer2'];
$qa->Answer3=$row['Answer3'];
$qa->Answer4=$row['Answer4'];
echo "<br />";
$rses=json_encode($qa);
echo $rses;
}
}else{
echo "error in connecting db";
}
?>
When I start my app it shows "this is null", but when I open my php code in browser it shows data correctly. 当我启动我的应用程序时,它显示“这是null”,但是当我在浏览器中打开php代码时,它可以正确显示数据。 What is the problem? 问题是什么?
UPDATE: 更新:
i used another url and its worked. 我用了另一个网址,它的工作。 so the problem is in my php codes.i removed echo "<br />";
所以问题出在我的PHP代码中。我删除了echo "<br />";
and opened it on browser.the result was {"Question":"question","Answer":"a","Answer2":"b","Answer3":"c","Answer4":"d"}
im not professional on json format but i think its not correct format.how can i fix this? 并在浏览器上将其打开。结果为{"Question":"question","Answer":"a","Answer2":"b","Answer3":"c","Answer4":"d"}
我不是专业的json格式,但我认为它的格式不正确。我该如何解决?
Your browser correctly renders HTML. 您的浏览器可以正确呈现HTML。 And this will be hidden to your eye 这将隐藏在您的眼中
echo "<br />";
Android cares solely about the string content, so it is unable to parse that break tag. Android仅关心字符串内容,因此无法解析该break标记。
You might inspect the logcat to see a JSONParseException is happening, though you catch it and return null, so the app doesn't crash 您可以检查logcat以查看JSONParseException发生了,尽管您捕获了它并返回null,所以应用程序不会崩溃
Only echo out the JSON in the PHP 只在PHP中回显JSON
And since you have a while loop, you want to build a JSONArray, and parse that instead of trying to parse a JsonObject line by line. 而且,由于有一个while循环,因此您想构建一个JSONArray并对其进行解析,而不是尝试逐行解析JsonObject。
Additionally, this isn't necessary 此外,这不是必需的
String message=gson.toJson(output);
You have a JsonObject as output
, so just toString
that 您有一个JsonObject作为output
,因此只需toString
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.