我正在android中构建这个演示项目以供临时使用。我需要在服务器数据库上运行查询并从中返回结果。我坚持从服务器部件中检索数据。这是我的档案 MainActivity:
df.format()
这是我的php脚本文件:
public void callit(View view) {
final String url = "http://xyz.000webhostapp.com/login.php?mail=abcxyz@gmail.com";
new Thread() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener tokener = new JSONTokener(json);
JSONArray finalResult = new JSONArray(tokener);
Toast.makeText(MainActivity.this, "This is my Toast message!",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
} catch (JSONException e) {
e.printStackTrace();
}
} else {
//Closes the connection.
try {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
} catch (IOException e) {
}
}
}
catch (ClientProtocolException e)
{
}
catch (IOException e)
e.printStackTrace();
{
}
}
}.start();
}
但是我不知道这些数据究竟是如何存储在json文件中的?从php文件中我可以看到,我只需要从数据库中返回给定电子邮件ID的移动电话号码。 PHP脚本工作正常,我在浏览器中测试它。
我只需要找到一种方法将此值发送到我的Android应用程序并烘烤该手机号码。
询问我的问题的任何部分是否含糊不清。抱歉英文不好。
答案 0 :(得分:2)
如果你在php中开发一个rest api,它会很好,here是一个附加休息的示例在线教程,或者如果它是一个学校项目那么安全(或正确地做事情)并不是一个问题只是你需要将它转换为json,只需在你的php文件中,创建一个简单的php页面,你可以在那里获得一个查询参数的电话号码$ email = $ _GET [" mail"];像你已经得到的这样,只需从android简单的抓取页面你需要将其转换为json 这里是简单的PHP程序,它回显电话号码和抓取该电话号码的java代码
<?php
echo "00923244549369"
?>
在您的代码中,您将在最后回复电话号码
public static void main(String args[]) {
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
url = new URL("http://localhost/writers/");
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception mue) {
mue.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
输出: 00923244549369