2016-04-11 17 views
0

使用API​​ 23.不能提供LogCat(我沒有一個模擬器,我只能通過生成apk來測試)。 我一直在嘗試這些一整天,我仍然卡住。 Android應用>發送一些字符串到位於我的本地主機(使用xampp)的PHP>取回這些字符串並在TextView中顯示。PHP不發回回/解決

現在工作,我終於搞定了,主要問題是從edittext中檢索文本時,它沒有正常加載。

public class MainActivity extends AppCompatActivity { 

Button b1; 
TextView t1; 
EditText e1; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 


    e1 = (EditText)findViewById(R.id.editText); 
    t1 = (TextView)findViewById(R.id.textView); 
    b1 = (Button) findViewById(R.id.button); 
    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try { 
      GetText(); 
      } catch (Exception ex) { 
       t1.setText("url exception"); 
      } 
     } 
    }); 
} 

public void GetText() throws UnsupportedEncodingException { 

    String nombre = e1.getText().toString(); 
    new SignIn().execute(nombre); 
} 

class SignIn extends AsyncTask<String,Void,String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String link="http://localhost:8012/kb_test/ejemplo.php"; // localhost = your actual ip (ipconfig at cmd) i use custom port 8012, use your own. 

     String code = params[0]; 
     StringBuilder sb = new StringBuilder(); 
     try { 
      String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(code, "UTF-8"); 

      URL url = new URL(link); 
      URLConnection conn = url.openConnection(); 

      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 

      wr.write(data); 
      wr.flush(); 

      BufferedReader reader = new BufferedReader(new InputStreamReader (conn.getInputStream())); 


      String line = null; 

      while((line = reader.readLine()) != null) 
      { 
       sb.append(line); 
       break; 
      } 
      // return sb.toString(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
      String aux = e.toString(); 
      return aux; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      String aux = e.toString(); 
      return aux; 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
      String aux = e.toString(); 
      return aux; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      String aux = e.toString(); 
      return aux; 
     } catch (Exception e) { 
      return new String("Exception: " + e.getMessage()); 
     } 
     return sb.toString(); 
    } 
    @Override 
    protected void onPostExecute(String result){ 
     Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG); 
     t1.setText(result); 
    } 
} 

的.xml

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/editText" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:text="Name" 
    android:ems="10" 
    android:id="@+id/editText" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Large Text" 
    android:id="@+id/textView" 
    android:layout_below="@+id/button" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="141dp" /> 

.PHP

<?php error_reporting(E_ALL); 


if (isset($_POST['name'])) { 
    $name = urldecode($_POST['name']); 
    echo 'Name: '.$name; } else { echo ' var error';}?> 

**我知道我必須進入我的實際工作在IP本地主機。 **添加了Internet權限。 感謝任何需要時間幫助的人。

+0

本地主機是我猜測不是你的服務器所在的Android設備? – RobVoisey

+0

也許http://stackoverflow.com/a/5806384/1240523? – RobVoisey

+0

你爲什麼要對$ _POST中的值進行urldecoding? DId你有一個額外的urlencode的內容? http://stackoverflow.com/questions/35516824/does-php-automatically-do-urldecode-on-post –

回答

0

你在你的PHP代碼中的錯誤,它應該是:

<?php 
    $name = urldecode($_GET['name']); 
    $user = urldecode($_GET['user']); 
    $email = urldecode($_GET['email']); 
    $pass = urldecode($_GET['pass']); 
    echo 'Name ' . $name . 'Email: ' . $email . ' User' . $user . ' Pass :' . $pass; 

你缺少每個字符串連接之間.。 如果仍然無法解決問題,請附上: error_reporting(E_ALL),並告訴我們當您嘗試訪問Web服務器上的腳本時出現什麼錯誤。

+0

注意:未定義指數:名稱在C:\ XAMPP \ htdocs中\ kb_test \ ejemplo.php上線3 說明:未定義指數:用戶在C:\ XAMPP \ htdocs中\ kb_test \ ejemplo.php第4行 通知:未定義的索引:第5行的C:\ xampp \ htdocs \ kb_test \ ejemplo.php中的電子郵件 注意:未定義的索引:通過C:\ xampp \ htdocs \ kb_test \ ejemplo.php在第6行 –

+0

看起來像你使用get方法來傳遞參數。相反'$ _POST'使用'$ _GET',並且都應該是好的。我爲你更新了代碼。再次檢查我的答案。 – Krac

+0

已經嘗試過,但出現同樣的錯誤。 –

0

廢話你沒有LogCat。當然,如果你已經將你的設備連接到你的電腦上,就有一個。

您不能在onClick()處理程序中調用getText()。由於所有網絡代碼都必須在AsyncTask或線程中完成。

這就是爲什麼你現在有NetworkOnMainThreadException

+0

感謝您的建議,我已經改變了我的代碼,以匹配從GUI檢索文本的正確方法,但我仍然有同樣的問題。不,我沒有LogCat。我只能通過構建和apk來測試代碼,然後轉移到真實的設備上。 –

+0

轉換爲真實設備是什麼意思?通常情況下,您可以使用USB電纜將設備連接至電腦。 IDE(Eclipse或Android Studio)會將您的應用程序上傳到設備。讓它運行。當然,這兩個IDE都有一個LogCat,您可以在其中跟蹤發生的事情。 – greenapps

+0

從gui中檢索文本的正確方法?你在說什麼?我們談到了互聯網連接的正確方法。在一個線程或AsyncTask中。現在你現在用它們中的一個嗎? – greenapps