こんにちわ、いつもお世話になります。
表記の通り
Android Studio からサーバー側にGETで文字列を追記するとFileNotFoundExceptionになってサーバー側にリクエストできないくて困ってます
下記の①のところで英数字は、サーバーにリクエストできるのですが文字だとできないです。
デバックすると②のところでFileNotFoundExceptionがでて、サーバーに送れないような感じがします。
何か解決策はあるのでしょうか?
ちなみにブラウザからだと
http://192.168.3.5:8080/book123/chapter14/search?keyword=あなご
リクエストできます。
Java
1public class MainActivity extends AppCompatActivity { 2 AsyncNetworkTask task; 3 EditText edit_text; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 task = new AsyncNetworkTask(this); 10 } 11 12 public void btn_onClick2(View view) { 13 String i = "yuusaku"; 14 String y = "あなご"; 15 16 //①//↓↓↓↓↓↓↓ここが問題 17 task.execute("http://192.168.3.5:8080/book123/chapter14/search?keyword="+y);//文字(あなご)だといけない 18 task.execute("http://192.168.3.5:8080/book123/chapter14/search?keyword="+i); //英数字はいける 19 } 20} 21 22//----------------------------------------------------------------------------------------------------------- 23 24public class AsyncNetworkTask extends AsyncTask<String, Integer, String> { 25 private TextView txtResult; 26 private ProgressBar progress; 27 28 public AsyncNetworkTask(Context context) { 29 super(); 30 MainActivity activity = (MainActivity)context; 31 txtResult = (TextView)activity.findViewById(R.id.txtResult); 32 progress = (ProgressBar)activity.findViewById(R.id.progress); 33 } 34 35 @Override 36 protected String doInBackground(String... params) { 37 publishProgress(30); 38 SystemClock.sleep(100); 39 publishProgress(30); 40 StringBuilder builder = new StringBuilder(); 41 try { 42 URL url = new URL(params[0]); 43 HttpURLConnection con = (HttpURLConnection)url.openConnection(); 44 con.setRequestMethod("GET"); 45 46 //②//ここのgetInputStream()が問題ではないか?? 47 /////yが「あなご」だとFileNotFoundExceptionとなり、サーバ側に送れてない気がする 48 BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); 49 String line; 50 while ((line = reader.readLine()) != null){ 51 builder.append(line); 52 } 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 publishProgress(100); 57 return builder.toString(); 58 } 59 @Override 60 protected void onPreExecute() { 61 progress.setVisibility(ProgressBar.VISIBLE); 62 } 63 64 @Override 65 protected void onProgressUpdate(Integer... values) { 66 // Log.d("url", values[0].toString()); 67 progress.setProgress(values[0]); 68 } 69 70 @Override 71 protected void onPostExecute(String result) { 72 txtResult.setText(result); 73 progress.setVisibility(ProgressBar.GONE); 74 } 75 76 @Override 77 protected void onCancelled() { 78 txtResult.setText("キャンセルされました。"); 79 progress.setVisibility(ProgressBar.GONE); 80 } 81} 82 83 84 85
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。