我正在开发一个个人项目,我需要将一些数据发送到数据库,数据库应该返回详细信息(采用JSON格式)。但是,我的应用程序没有收到服务器的任何响应(它返回一个空值)。有谁能够帮我?谢谢。以下是我的代码:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.concurrent.RecursiveTask;
public class InventoryBackground extends AsyncTask<String, Void, String> {
String inventory_url = "http://10.100.50.19/inventory.php";
Context ctx;
ProgressDialog progressDialog;
Activity activity;
AlertDialog.Builder builder;
public InventoryBackground(Context ctx){
this.ctx = ctx;
activity = (Activity) ctx;
}
@Override
protected void onPreExecute() {
builder = new AlertDialog.Builder(activity);
progressDialog = new ProgressDialog(ctx);
progressDialog.setTitle("Please Wait");
progressDialog.setMessage("Searching From Database...");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
try{
URL url = new URL(inventory_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String part_id;
part_id = params[0];
String data = URLEncoder.encode("partid","UTF-8") +"="+ URLEncoder.encode(part_id,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while((line = bufferedReader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
httpURLConnection.disconnect();
Thread.sleep(5000);
return stringBuilder.toString().trim();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); }
@Override
protected void onPostExecute(String json) {
try{
progressDialog.dismiss();
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("server_response");
JSONObject JO = jsonArray.getJSONObject(0);
String code = JO.getString("code");
System.out.println("Code = " + code);
String message_inv_partno = JO.getString("message_inv_partno");
if (code.equals("partid_true"))
{
TextView partID = (TextView) activity.findViewById(R.id.tv_inventory_data_partno);
partID.setText(message_inv_partno);
}
else if (code.equals("partid_false")) {
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}