这是我正在使用的代码的总更新,减去一堆诸如转盘按钮意图之类的非相关内容。也许您可以看到我做错的更好。
我的php我应该注意,我是编码的新手,这是我第一次尝试从mysql下载PDO。我可以使用PDO上传。看起来好像做对了,但是由于崩溃,我目前无法判断。
非常感谢您的帮助。
<?php
require("conn.php");
$ID = ["ptID"];
try{
$sql = 'SELECT x FROM table WHERE ID = :ID';
$stmt = $conn->prepare($sql);
$stmt->execute(array('ptID'=> $ID));
$posts = $stmt->fetchALL();
$result = mysql_query($conn, $sql);
if ($row = mysql_fetch_assoc($result) > 0)
{
$row = mysqli_fetch_assoc($result);
$x = $row["x"];
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
$conn = null;
?>
这是我的活动课程ctest
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class cervicaltest extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
//whole bunch of spinners, to collect user input all of which functions.
tests prior to adding the code to get data worked find.
}
//gets the key to the database for the upload.
private void useID() {
SharedPreferences shareID = getSharedPreferences("shareID", Context.MODE_PRIVATE);
ptID = shareID.getString("ID", "");
}
public void settest() {
//bunch of if else statements using the user input. all of which worked
prior to trying to get data.
}
// both of these were left blank as I never used them.
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
//this is the get data class.
public void getrot() {
gettype ="getcrom";
downloadbackgroundworker Downloadbackgroundworker = new downloadbackgroundworker(this, new downloadbackgroundworker.AsyncResponse() {
@Override
public void processfinish(String response) {
rcervrot = response;
}
});
Downloadbackgroundworker.execute(gettype, ptID);
}
public void testsaveClick(View view) {
x = x.getSelectedItem().toString();
type = "testdb";
useID();
getrot();
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, ptID, x //bunch of variables);
// Rcervicalticcalcu(); these were marketed out intents that I had used to get data previously but didn't like the result. all the code worked, but I kept getting kicked to this activity with the intent and I didn't want to end up at this activity. Everything worked good when transfering data as an intent. So somehow it's getting the data from the database that is crashing me. all the code for the intents is marked out.
//Lcervicalticcalcu();
settest();
}
}
最后,这是getdata的downloadworker类。上传背景工作人员之前工作正常,但我没有做任何更改。
这是downloadworker类
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
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;
public class downloadbackgroundworker extends AsyncTask<String, Void, String> {
AlertDialog downloadalert;
Context dlctx;
downloadbackgroundworker (Context dlctx, AsyncResponse asyncResponse) {
this.dlctx = dlctx;
delegate = asyncResponse;
}
@Override
protected void onPreExecute(){
downloadalert = new AlertDialog.Builder(dlctx).create();
downloadalert.setTitle("retrieve Data");
}
@Override
protected String doInBackground(String... params){
String gettype = params[0];
String cervrotgeturl = "http://IP/phpfile";
if(gettype.equals("getcrom"))
{
String ID = params[1];
try {
URL url = new URL(cervrotgeturl);
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 data = URLEncoder.encode("ptID", "UTF-8") + "=" + URLEncoder.encode(ID, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String response ="";
String line = "";
while ((line = bufferedReader.readLine())!=null){
response = line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public AsyncResponse delegate = null;
public interface AsyncResponse {
void processfinish (String response);
}
@Override
protected void onProgressUpdate(Void... values){
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String response) {
delegate.processfinish(response);
}
}
答案 0 :(得分:1)
public AsyncResponse delegate=null;
public interface AsyncResponse {
void processFinish(Boolean output);
}
getBackgroundworker (Context ctx, AsyncResponse asyncResponse) {
this.ctx =ctx;
delegate = asyncResponse;
}
getBackgroundworker(Context ctx)
{
this.ctx =ctx;
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
delegate.processFinish(stuff);
//I am passing the boolean variable here. You can pass accordingly
}
现在在您要使用填充变量值的活动中。您必须实现AsyncResponse接口并覆盖其方法。