2016-11-07 9 views
1

自分のサーバーでモバイルアプリケーションを作成しようとしています。私が書いたServerは、Laravelフレームワークを持つPHP上にあり、ローカルマシン上のxamppによってホストされています。私は電話を介して接続することができます - 私のローカルサイトを開くためにブラウザ。しかし、私はモバイルアプリケーションを作成し、HttpUrlConnection経由でサーバーにアクセスしたいと思います。POSTリクエスト500アンドロイドデバイスのローカルサーバー - ローカルサーバー

私のブラウザですべて正常に動作します。しかし私のPOSTリクエストは私に500コードを与えます。私は、問題はリクエストのプロパティにあると思うが、何を書くべきか分からない。

マイアンドロイド - ポスト - 要求コード:

JSONObject jsonObject = null; 
    URL url = null; 
    try { 
       jsonObject = new JSONObject(); 
       jsonObject.put("name", bundle.getString("name")); 
       jsonObject.put("password", bundle.getString("password")); 
       jsonObject.put("email", bundle.getString("email")); 


       url = new URL("http://192.168.1.101:8081/UserAdd"); 

      } 
      catch(Exception ex){ 
       ex.printStackTrace(); 
      } 
try { 
     byte[] bytes = jsonObject.toString().getBytes("UTF-8"); 
     urlConnection = (HttpURLConnection) 
       url.openConnection(); 
     urlConnection.setRequestMethod("POST"); 
     // urlConnection.setRequestProperty("Cookie", cookies); 
     urlConnection.setRequestProperty("Content-Type", "application/json"); 
     urlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); 
     urlConnection.setRequestProperty("Host", "localhost:8081"); 
     urlConnection.setRequestProperty("Content-Length", String.valueOf(bytes.length)); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     if (flag) { 
      urlConnection.setInstanceFollowRedirects(false); 
     } 

     OutputStream os = urlConnection.getOutputStream(); 
     /*BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(os, "UTF-8")); 
     writer.write(jsonObject.toString()); 
     writer.flush(); 
     writer.close();*/ 
     os.write(jsonObject.toString().getBytes()); 
     os.flush(); 
     os.close(); 

     urlConnection.connect(); 


     boolean redirect = false; 

     int status = urlConnection.getResponseCode(); 
     if (status != HttpURLConnection.HTTP_OK) { 
      if (status == HttpURLConnection.HTTP_MOVED_TEMP 
        || status == HttpURLConnection.HTTP_MOVED_PERM 
        || status == HttpURLConnection.HTTP_SEE_OTHER) 
       redirect = true; 
     } 

     if (redirect) { 
      String newUrl = urlConnection.getHeaderField("Location"); 
      String cookies = urlConnection.getHeaderField("Set-Cookie"); 

     } 

     InputStream inputStream = urlConnection.getInputStream(); 
     StringBuffer buffer = new StringBuffer(); 
     reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      buffer.append(line); 
     } 
     resultJson = buffer.toString(); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

コンピュータブラウザからマイネットワーク要求ヘッダー。私はこのクッキーフィールドが必要だと思うが、それを見つける場所はわからない。私はとても新しいです。私はあなたの助けを求めています。

enter image description here

+0

まあまあです。呼び出すPHPファイルに構文エラーがあります。 –

+0

しかし、そのファイルとルートは、コンピュータとモバイルブラウザで動作しています。あなたの推測で私が誤解したことがありますか? @Nandan –

+0

http://192.168.1.101:8081/UserAddはあなたのxampサーバーのローカルIPですか? –

答えて

1

私はあなたのバックエンドのスクリプトについては考えているが、必ず以下のものを作るんので、私は、わかりません。

  1. あなたはルートのためにPOSTを使用しています。ブラウザを使用してページを読み込むことができるので、GETを使用していることは疑問です。例:

    Route::post('UserAdd', '[email protected]'); 
    
  2. コントローラ/ PHPファイルにエラーがないことをもう一度確認します。

  3. authControllerを使用している場合は、有効なアクセス許可があるかどうかを確認してください。

  4. .htaccessファイルを確認してください。

関連する問題