Java 未从联机mysql服务器检索数据
我试图通过json从在线数据库(MySql)获取数据,并希望将其显示为ListView。到目前为止我所做的: Downloader.javaJava 未从联机mysql服务器检索数据,java,php,android,mysql,database,Java,Php,Android,Mysql,Database,我试图通过json从在线数据库(MySql)获取数据,并希望将其显示为ListView。到目前为止我所做的: Downloader.java public class Downloader extends AsyncTask<Void,Integer,String> { Context c; String address; ListView lv; ProgressDialog pd; public Downloader(Context c, String address, List
public class Downloader extends AsyncTask<Void,Integer,String> {
Context c;
String address;
ListView lv;
ProgressDialog pd;
public Downloader(Context c, String address, ListView lv) {
this.c = c;
this.address = address;
this.lv = lv;
}
//B4 JOB STARTS
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("Fetch Data");
pd.setMessage("Fetching Data...Please wait");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
String data=downloadData();
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();;
if(s != null)
{
Parser p=new Parser(c,s,lv);
p.execute();
}else
{
Toast.makeText(c,"Unable to download data",Toast.LENGTH_SHORT).show();
}
}
private String downloadData()
{
//connect and get a stream
InputStream is=null;
String line =null;
try {
URL url=new URL(address);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
is=new BufferedInputStream(con.getInputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuffer sb=new StringBuffer();
if(br != null) {
while ((line=br.readLine()) != null) {
sb.append(line+"n");
}
}else {
return null;
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is != null)
{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public class MainActivity extends Activity {
String url="http://bookvilla.esy.es/book.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv= (ListView) findViewById(R.id.lv);
final Downloader d=new Downloader(this,url,lv);
d.execute();
}
}
当我运行应用程序时,应用程序。。在Downloader.java类的第35行,我收到一条toast消息,上面写着“无法下载数据”。所以我猜我从数据库中得到的数据是空的…但为什么是空的
$con=mysqli_connect($host、$u_name、$pwd、$db)或die('Unable to connect');如果(mysqli_connect_error($con)){echo“无法连接到
数据库“.mysqli_connect_error()
}
$query=mysqli_query($con,“从playerstb中选择*);如果($query){
而($row=mysqli_fetch_数组($query)){$flag[]=$row
}打印(json_encode($flag))}mysqli_close($con)
上面是我的php文件
不确定如何解决您的问题,但我使用了不同的方法: db_config.php:
<?php
define('DB_USER',"root");
define('DB_PASSWORD',null);
define('DB_DATABASE',"phoneshop");
define('DB_SERVER',"localhost");
?>
和解析器:
public class JSONParser {
static JSONObject jObj;
static String json;
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public static JSONObject makeHttpRequest(String url, String method, ContentValues params) {
// Making HTTP request
try {
final OkHttpClient client = new OkHttpClient();
Request request;
// check for request method
if (method.equals("POST")) {
// request method is POST
MediaType contentType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
String content = "";
for (String key : params.keySet())
{
if ( !content.isEmpty())
content += "&";
content += key + "=" + params.get(key);
}
RequestBody body = RequestBody.create(contentType, content);
request = new Request.Builder().url(url).post(body).build();
}
else {
// request method is GET
request = new Request.Builder().url(url).build();
}
final Response response = client.newCall(request).execute();
json = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e ){
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
将此项添加到渐变时:
compile 'com.squareup.okhttp3:okhttp:3.4.2'
没有人帮忙吗?
private String downloadData()
甚至被调用了吗?我不这么认为。我想你忘了覆盖public void execute()
MethodBro我解决了我的错误…那是一个愚蠢的错误…我把“http://”放了两次..哈哈。无论如何,谢谢你抽出时间。你把它放在哪里了?在MainActivity.java的第二行…你不会看到它,因为我已经编辑了这个问题。
private String url_all_products = "http://10.0.0.10:8012/onlineShop/get_all_products.php";
//ip of the server or 10.0.2.2 if in emulator
ContentValues params = new ContentValues();
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
public class JSONParser {
static JSONObject jObj;
static String json;
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public static JSONObject makeHttpRequest(String url, String method, ContentValues params) {
// Making HTTP request
try {
final OkHttpClient client = new OkHttpClient();
Request request;
// check for request method
if (method.equals("POST")) {
// request method is POST
MediaType contentType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
String content = "";
for (String key : params.keySet())
{
if ( !content.isEmpty())
content += "&";
content += key + "=" + params.get(key);
}
RequestBody body = RequestBody.create(contentType, content);
request = new Request.Builder().url(url).post(body).build();
}
else {
// request method is GET
request = new Request.Builder().url(url).build();
}
final Response response = client.newCall(request).execute();
json = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e ){
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
compile 'com.squareup.okhttp3:okhttp:3.4.2'