私はAndroid Devには新しく、私はGoogleフォームを作成してアプリケーションにフォームを実装したいと思っていましたが、オープンソースのオープンソースokhttp私はフォームとすべてのコードを挿入し、一切のエラーと呼ばれるJavaクラスを作成し ...だからOkHttpが正常に動作しない
を知って、私は彼らに同じIDとすべてのものを与えた、ラベル付きレイアウトを作成しました。
修正するためにあったすべてのものを実装し、修正後だから、私は、アプリケーションを実行しましたが、それだけで何かをdoesntの
(影響か、ではないが、その場合、私は、ナビゲーションドロワーアクティビティを使用しています言及する価値、知りません) 。妥当性検査も応答も送信しません。
誰でも私は、もう一度感謝をここに
コードを残します。この
で私を助けることができれば、私は本当に感謝。
package com.example.eduardobastos.testapp;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class Form extends AppCompatActivity {
public static final MediaType FORM_DATA_TYPE
= MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
//URL derived from form URL
public static final String URL="https://docs.google.com/forms/d/e/1FAIpQLSeZp9wjprZJ3OR2SkIHHsZE9yDBAVnC7mO8hPKSzwGuYhqmdw/formResponse";
//input element ids found from the live form page
public static final String EMAIL_KEY="entry_943499687";
public static final String SUBJECT_KEY="entry_2058392291";
public static final String MESSAGE_KEY="entry_1420026128";
private Context context;
private EditText emailEditText;
private EditText subjectEditText;
private EditText messageEditText;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.form_layout);
//save the activity in a context variable to be used afterwards
context =this;
//Get references to UI elements in the layout
Button sendButton = (Button)findViewById(R.id.sendButton);
emailEditText = (EditText)findViewById(R.id.emailEditText);
subjectEditText = (EditText)findViewById(R.id.subjectEditText);
messageEditText = (EditText)findViewById(R.id.messageEditText);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Make sure all the fields are filled with values
if(TextUtils.isEmpty(emailEditText.getText().toString()) ||
TextUtils.isEmpty(subjectEditText.getText().toString()) ||
TextUtils.isEmpty(messageEditText.getText().toString()))
{
Toast.makeText(context,"All fields are mandatory.",Toast.LENGTH_LONG).show();
return;
}
//Check if a valid email is entered
if(!android.util.Patterns.EMAIL_ADDRESS.matcher(emailEditText.getText().toString()).matches())
{
Toast.makeText(context,"Please enter a valid email.",Toast.LENGTH_LONG).show();
return;
}
//Create an object for PostDataTask AsyncTask
PostDataTask postDataTask = new PostDataTask();
//execute asynctask
postDataTask.execute(URL,emailEditText.getText().toString(),
subjectEditText.getText().toString(),
messageEditText.getText().toString());
}
});
}
//AsyncTask to send data as a http POST request
private class PostDataTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... contactData) {
Boolean result = true;
String url = contactData[0];
String email = contactData[1];
String subject = contactData[2];
String message = contactData[3];
String postBody="";
try {
//all values must be URL encoded to make sure that special characters like & | ",etc.
//do not cause problems
postBody = EMAIL_KEY+"=" + URLEncoder.encode(email,"UTF-8") +
"&" + SUBJECT_KEY + "=" + URLEncoder.encode(subject,"UTF-8") +
"&" + MESSAGE_KEY + "=" + URLEncoder.encode(message,"UTF-8");
} catch (UnsupportedEncodingException ex) {
result=false;
}
try{
//Create OkHttpClient for sending request
OkHttpClient client = new OkHttpClient();
//Create the request body with the help of Media Type
RequestBody body = RequestBody.create(FORM_DATA_TYPE, postBody);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
//Send the request
Response response = client.newCall(request).execute();
}catch (IOException exception){
result=false;
}
return result;
}
@Override
protected void onPostExecute(Boolean result){
//Print Success or failure message accordingly
Toast.makeText(context,result?"Message successfully sent!":"There was some error in sending message. Please try again after some time.",Toast.LENGTH_LONG).show();
}
}
}
あなたは、デバッグしようとしたことがありますか?それはタスクのdoInBackgroundメソッドに入りますか? –
こんにちはJav T.私は4/5hでそれを試みます。私はちょうど寝ました。それはここ6:30です。あなたは私がチェックしなければならないこと、またはそれが間違っている/良くないことに気付かれましたか? –
私はデバッグ操作について投稿しておいてください。また、そのタスククラスの使用に関するいくつかのヒントを教えてください;) –