我的应用中有一个按钮,当我点击它时,它会声明方法insertintodatabase
。但是,当我点击它时,没有任何事情发生,即使log
没有显示任何内容。
问题出在哪儿 ?请建议。
private final OkHttpClient client = new OkHttpClient();
public void SignUp(View view)
{
insertToDatabase();
}
private void insertToDatabase(){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute()
{
name = usernam.getText().toString();
pass = passw.getText().toString();
emails = email.getText().toString();
Log.e("GetText","called");
}
@Override
protected String doInBackground(String... params) {
String json = "";
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", name);
jsonObject.accumulate("password", pass);
jsonObject.accumulate("email", emails);
json = jsonObject.toString();
Log.e("MYAPP", "getjson");
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
try{
RequestBody formBody = new FormEncodingBuilder()
.add("result", json)
.build();
Request request = new Request.Builder()
.url("https://justedhak.comlu.com/receiver.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
} catch (IOException e){
Log.e("MYAPP", "unexpected JSON exception", e);
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
php
$username= $_POST['username'];
$password= $_POST['password'];
$email= $_POST['email'];
$image =$_POST['image'];
$sql = "insert into USERS (username,password,email) values ('$username','$password','$email')";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
mysqli_close($con);
答案 0 :(得分:1)
某处你必须创建SendPostReqAsyncTask的对象并调用execute方法传递参数......
// Example class
class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
// some code
}
// Call the execute method of Async Task
new DownloadFilesTask().execute(url1, url2, url3);
答案 1 :(得分:1)
//像这样使用..
public class <Your Root class> extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
// some code
private final OkHttpClient client = new OkHttpClient();
public void SignUp(View view)
{
new SendPostReqAsyncTask().execute();
}
}
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute()
{
name = usernam.getText().toString();
pass = passw.getText().toString();
emails = email.getText().toString();
Log.e("GetText","called");
}
@Override
protected String doInBackground(String... params) {
String json = "";
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", name);
jsonObject.accumulate("password", pass);
jsonObject.accumulate("email", emails);
json = jsonObject.toString();
Log.e("MYAPP", "getjson");
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
try{
RequestBody formBody = new FormEncodingBuilder()
.add("result", json)
.build();
Request request = new Request.Builder()
.url("https://justedhak.comlu.com/receiver.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
} catch (IOException e){
Log.e("MYAPP", "unexpected JSON exception", e);
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
答案 2 :(得分:1)
因为OkHttp支持异步,我想你也可以参考以下方式:
我们假设您mHandler = new Handler(Looper.getMainLooper());
onCreate
private void updateToDatabase() {
// POST request
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormEncodingBuilder()
.add("key1", "value1")
.add("key2", "value2")
.build();
Request request = new Request.Builder()
.url("http://...")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(final Request request, final IOException e) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, e.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Response response) throws IOException {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, response.body().string(), Toast.LENGTH_SHORT).show();
}
});
}
});
}
如果您仍想使用AsyncTask,请按以下步骤更新您的代码:
public class MainActivity extends AppCompatActivity {
...
public void SignUp(View view)
{
new SendPostReqAsyncTask().execute();
}
...
private class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
name = usernam.getText().toString();
pass = passw.getText().toString();
emails = email.getText().toString();
Log.e("GetText", "called");
}
@Override
protected String doInBackground(String... params) {
String json = "";
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", name);
jsonObject.accumulate("password", pass);
jsonObject.accumulate("email", emails);
json = jsonObject.toString();
Log.e("MYAPP", "getjson");
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
try {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormEncodingBuilder()
.add("result", json)
.build();
Request request = new Request.Builder()
.url("https://justedhak.comlu.com/receiver.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
} catch (IOException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
}