アプリにある文字をサーバに上げたい。
今、androidアプリを作っています。
文字の読込はできていると思いますが、
通信のやりかた、コードの書き方が合っているのか、わからない状況です。
アプリ開発超初心者なので、大変恐縮ですが
できるだけわかりやすいものをよろしくお願いいたします。
http://(adress) のwebサーバに上げていきたいです。
先生が持っているサーバをお借りしたという形です。
ファイルにしなくても書き込みでもいいので、どんどん情報が追加されるような
ものにしたいです。
コードが合っているのであれば、サーバ自体に自分が上手くアクセスできていない可能性があるかなと思うので、アクセス権限などを確認します。
調べてみるとPOST通信とかあるらしく、ネットからの情報で書いてみていますが、理解に苦しみました。
ご教授よろしくお願いします
java
1package com.example.post_1009; 2 3import androidx.appcompat.app.AppCompatActivity; 4 5import android.os.Bundle; 6import androidx.appcompat.app.AppCompatActivity; 7//import android.support.v7.app.AppCompatActivity; 8 9import android.content.Intent; 10import android.net.Uri; 11import android.os.Bundle; 12import android.view.View; 13import android.widget.Button; 14import android.widget.EditText; 15import android.widget.TextView; 16 17 18 19public class MainActivity extends AppCompatActivity { 20 21 private UploadTask task; 22 private TextView textView; 23 // wordを入れる 24 private EditText editText; 25 26 // phpがPOSTで受け取ったwordを入れて作成するHTMLページ(適宜合わせてください) 27 String url = "http://address/"; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 34 editText = findViewById(R.id.uriname); 35 36 Button post = findViewById(R.id.post); 37 38 // ボタンをタップして非同期処理を開始 39 post.setOnClickListener(new View.OnClickListener() { 40 @Override 41 public void onClick(View v) { 42 String param0 = editText.getText().toString(); 43 44 if(param0.length() != 0){ 45 task = new UploadTask(); 46 task.setListener(createListener()); 47 task.execute(param0); 48 } 49 50 } 51 }); 52 53 // ブラウザを起動する 54 Button browser = findViewById(R.id.browser); 55 browser.setOnClickListener(new View.OnClickListener() { 56 @Override 57 public void onClick(View v) { 58 // phpで作成されたhtmlファイルへアクセス 59 Uri uri = Uri.parse(url); 60 Intent intent = new Intent(Intent.ACTION_VIEW,uri); 61 startActivity(intent); 62 63 // text clear 64 textView.setText(""); 65 } 66 }); 67 68 textView = findViewById(R.id.text_view); 69 } 70 71 72 @Override 73 protected void onDestroy() { 74 task.setListener(null); 75 super.onDestroy(); 76 } 77 78 private UploadTask.Listener createListener() { 79 return new UploadTask.Listener() { 80 @Override 81 public void onSuccess(String result) { 82 textView.setText(result); 83 } 84 }; 85 } 86}
java
1package com.example.post_1009; 2import android.os.AsyncTask; 3import android.util.Log; 4import java.io.IOException; 5import java.io.OutputStream; 6import java.net.HttpURLConnection; 7import java.net.URL; 8import java.nio.charset.StandardCharsets; 9 10 11public class UploadTask extends AsyncTask<String, Void, String> { 12 13 private Listener listener; 14 15 // 非同期処理 16 @Override 17 protected String doInBackground(String... params) { 18 19 // 使用するサーバーのURLに合わせる 20 String urlSt = "http://address/"; 21 22 HttpURLConnection httpConn = null; 23 String result = null; 24 String word = "word="+params[0]; 25 26 try{ 27 // URL設定 28 URL url = new URL(urlSt); 29 30 // HttpURLConnection 31 httpConn = (HttpURLConnection) url.openConnection(); 32 33 // request POST 34 httpConn.setRequestMethod("POST"); 35 36 // no Redirects 37 httpConn.setInstanceFollowRedirects(false); 38 39 // データを書き込む 40 httpConn.setDoOutput(true); 41 42 // 時間制限 43 httpConn.setReadTimeout(10000); 44 httpConn.setConnectTimeout(20000); 45 46 // 接続 47 httpConn.connect(); 48 49 try(// POSTデータ送信処理 50 OutputStream outStream = httpConn.getOutputStream()) { 51 outStream.write( word.getBytes(StandardCharsets.UTF_8)); 52 outStream.flush(); 53 Log.d("debug","flush"); 54 } catch (IOException e) { 55 // POST送信エラー 56 e.printStackTrace(); 57 result = "POST送信エラー"; 58 } 59 60 final int status = httpConn.getResponseCode(); 61 if (status == HttpURLConnection.HTTP_OK) { 62 // レスポンスを受け取る処理等 63 result="HTTP_OK"; 64 } 65 else{ 66 result="status="+String.valueOf(status); 67 } 68 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } finally { 72 if (httpConn != null) { 73 httpConn.disconnect(); 74 } 75 } 76 return result; 77 } 78 79 // 非同期処理が終了後、結果をメインスレッドに返す 80 @Override 81 protected void onPostExecute(String result) { 82 super.onPostExecute(result); 83 84 if (listener != null) { 85 listener.onSuccess(result); 86 } 87 } 88 89 void setListener(Listener listener) { 90 this.listener = listener; 91 } 92 93 interface Listener { 94 void onSuccess(String result); 95 } 96} 97
xml
1<?xml version="1.0" encoding="utf-8"?> 2<!--AndroidX--> 3<androidx.constraintlayout.widget.ConstraintLayout 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 xmlns:tools="http://schemas.android.com/tools" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 android:background="#fdc" 10 tools:context=".MainActivity"> 11 12 <TextView 13 android:id="@+id/word" 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:layout_marginEnd="10dp" 17 android:text="@string/word" 18 android:textSize="24sp" 19 app:layout_constraintBottom_toBottomOf="parent" 20 app:layout_constraintEnd_toEndOf="@+id/guideline" 21 app:layout_constraintLeft_toLeftOf="parent" 22 app:layout_constraintTop_toTopOf="parent" 23 app:layout_constraintVertical_bias="0.15" /> 24 25 <EditText 26 android:id="@+id/uriname" 27 android:layout_width="0dp" 28 android:layout_height="wrap_content" 29 android:autofillHints="@string/hint" 30 android:background="#fff" 31 android:hint="@string/hint" 32 android:inputType="textUri" 33 android:textSize="24sp" 34 app:layout_constraintStart_toStartOf="@+id/guideline" 35 app:layout_constraintBottom_toBottomOf="parent" 36 app:layout_constraintRight_toRightOf="parent" 37 app:layout_constraintTop_toTopOf="parent" 38 app:layout_constraintVertical_bias="0.15" /> 39 40 <Button 41 android:id="@+id/post" 42 android:layout_width="0dp" 43 android:layout_height="wrap_content" 44 android:layout_marginStart="30dp" 45 android:layout_marginEnd="10dp" 46 android:text="@string/post" 47 app:layout_constraintTop_toTopOf="parent" 48 app:layout_constraintRight_toLeftOf="@+id/browser" 49 app:layout_constraintLeft_toLeftOf="parent" 50 app:layout_constraintBottom_toBottomOf="parent" 51 app:layout_constraintVertical_bias="0.3" /> 52 53 <Button 54 android:id="@+id/browser" 55 android:layout_width="0dp" 56 android:layout_height="wrap_content" 57 android:layout_marginEnd="30dp" 58 android:layout_marginStart="10dp" 59 android:text="@string/browser" 60 app:layout_constraintLeft_toRightOf="@+id/post" 61 app:layout_constraintTop_toTopOf="parent" 62 app:layout_constraintBottom_toBottomOf="parent" 63 app:layout_constraintRight_toRightOf="parent" 64 app:layout_constraintVertical_bias="0.3" /> 65 66 <TextView 67 android:id="@+id/text_view" 68 android:layout_width="wrap_content" 69 android:layout_height="wrap_content" 70 android:textSize="24sp" 71 app:layout_constraintTop_toTopOf="parent" 72 app:layout_constraintRight_toRightOf="parent" 73 app:layout_constraintLeft_toLeftOf="parent" 74 app:layout_constraintBottom_toBottomOf="parent" 75 app:layout_constraintVertical_bias="0.4" /> 76 <!--AndroidX--> 77 <androidx.constraintlayout.widget.Guideline 78 android:id="@+id/guideline" 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:orientation="vertical" 82 app:layout_constraintGuide_begin="100dp" /> 83 84</androidx.constraintlayout.widget.ConstraintLayout>
これで、エラーは出ていません。
あなたの回答
tips
プレビュー