2014-02-21 49 views
0

我只是把過程搞糊塗了。我想將TextView和Edittext1 & Edittect2的值傳遞給Second.java,並且通過使用httpget想要從login.php獲取數據,並在onclick事件期間將輸出提供給主類....幫助我,,,在此先感謝。我是Android領域的新手。我想從HTTP獲取HTTP請求,使用asyntask

package com.example.messagebox; 
import java.io.BufferedReader; 
import org.apache.http.HttpResponse; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
public class MessageBox extends Activity { 
    private static final String USER_AGENT = null; 
    private static String username,password,role; 
    //private String link="http://192.168.1.12:81/arun/loginpost.php"; 
    private static String result, user , pass; 
    private static HttpResponse response; 
    private static BufferedReader in; 
    private static StringBuffer sb; 
    private EditText message,date; 
    private TextView message_view,tview; 
    private Button btn; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.message_box); 
     message = (EditText)findViewById(R.id.message); 
     date = (EditText)findViewById(R.id.date); 
     message_view = (TextView)findViewById(R.id.textView_msg); 
     tview = (TextView)findViewById(R.id.output_text); 

     btn=(Button) findViewById(R.id.button1); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.message_box, menu); 
     return true; 
    } 
    private void login() { 

    } 
} 

它是我的AsyncTask

package com.example.messagebox; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URI; 
import java.net.URL; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.widget.TextView; 
public class Second extends AsyncTask<String, Void, String>{ 
    public Second(Context context,TextView statusField) 
    { 

    } 
    @Override 
    protected String doInBackground(String... params) { 
     try{ 

       String username = "arun"; 
       String password = "arun"; 
       String link = "http://192.168.1.12:81/arun/login.php?username=" 
       +username+"&password="+password; 
       URL url = new URL(link); 
       HttpClient client = new DefaultHttpClient(); 
       HttpGet request = new HttpGet(); 
       request.setURI(new URI(link)); 
       HttpResponse response = client.execute(request); 
       BufferedReader in = new BufferedReader 
       (new InputStreamReader(response.getEntity().getContent())); 

       StringBuffer sb = new StringBuffer(""); 
       String line=""; 
       while ((line = in.readLine()) != null) { 
        sb.append(line); 
        break; 
       } 
       in.close(); 
       return sb.toString(); 

      }catch(Exception e){ 
      return new String("Exception: " + e.getMessage()); 
      } 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
    } 



} 

second.java類這是我的XML

<EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/editText1" 
     android:layout_below="@+id/editText1" 
     android:layout_marginTop="25dp" 
     android:ems="10" 
     android:inputType="textPassword" > 
    </EditText> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="44dp" 
     android:ems="10" > 

    <requestFocus android:layout_width="wrap_content" /> 

    </EditText> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_alignParentLeft="true" 
     android:text="@string/Username" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/App" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView5" 
     android:layout_alignLeft="@+id/textView6" 
     android:text="@string/Role" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textSize="10sp" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView6" 
     android:layout_marginTop="27dp" 
     android:layout_toLeftOf="@+id/editText1" 
     android:text="@string/LoginRole" /> 
    <TextView 
     android:id="@+id/textView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView6" 
     android:layout_alignLeft="@+id/textView5" 
     android:layout_marginBottom="27dp" 
     android:text="@string/method" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView8" 
     android:layout_below="@+id/button1" 
     android:layout_marginTop="86dp" 
     android:text="@string/LoginStatus" /> 


    <TextView 
     android:id="@+id/textView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/textView4" 
     android:layout_centerHorizontal="true" 
     android:text="@string/Status" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textSize="10sp" /> 

    <TextView 
     android:id="@+id/textView9" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView8" 
     android:layout_alignLeft="@+id/textView6" 
     android:text="@string/Choose" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textSize="10sp" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/textView6" 
     android:onClick="loginPost" 
     android:text="@string/LoginPost" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button2" 
     android:layout_alignBottom="@+id/button2" 
     android:layout_alignLeft="@+id/textView2" 
     android:onClick="login" 
     android:text="@string/LoginGet" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText2" 
     android:layout_alignBottom="@+id/editText2" 
     android:layout_alignParentLeft="true" 
     android:text="@string/Password" /> 

</RelativeLayout> 
+0

不要使用AsyncTask作爲單獨的類。在MainActivity中使用這個類 –

+0

我也試過,但它顯示android.os.networkexception錯誤 –

+0

,因爲你沒有在AndroidManifest.xml文件中添加INTERNET權限。 –

回答

0

也許這個代碼將幫助你。

public class MessageBox extends Activity { 
private static final String USER_AGENT = null; 
private static String username,password,role; 
//private String link="http://192.168.1.12:81/arun/loginpost.php"; 
private static String result, user , pass; 
private static HttpResponse response; 
private static BufferedReader in; 
private static StringBuffer sb; 
private EditText message,date; 
private TextView message_view,tview; 
private Button btn; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.message_box); 
    message = (EditText)findViewById(R.id.message); 
    date = (EditText)findViewById(R.id.date); 
    message_view = (TextView)findViewById(R.id.textView_msg); 
    tview = (TextView)findViewById(R.id.output_text); 

    btn=(Button) findViewById(R.id.button1); 
    btn.setOnClickListner((OnClickListener) this); 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.message_box, menu); 
    return true; 
} 

private void onClick(View v) { 
new sendPostData().execute(); 
} 

private class sendPostData extends AsyncTask<String, Void, String> 
{ 
     @Override 
    protected String doInBackground(String... params) { 

String line=""; 

try{ 

      String username = "arun"; 
      String password = "arun"; 
      String link = "http://192.168.1.12:81/arun/login.php?username=" 
      +username+"&password="+password; 
      URL url = new URL(link); 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(); 
      request.setURI(new URI(link)); 
      HttpResponse response = client.execute(request); 
      BufferedReader in = new BufferedReader 
      (new InputStreamReader(response.getEntity().getContent())); 

      StringBuffer sb = new StringBuffer(""); 

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

     }catch(Exception e){ 
     return new String("Exception: " + e.getMessage()); 
     } 

    } 
     @Override 
    protected void onPostExecute(String result) { 
     //View your result here. 
      message_view.setText(line); 
     } 
} 

    } 

而且不要忘了加在的Manifest.xml許可使用PHP這個查詢文件

<uses-permission android:name="android.permission.INTERNET" /> 
+0

它在btn.setOnClickListener(this)中顯示錯誤; 類型View中的setOnClickListener(View.OnClickListener)方法不適用於參數(MessageBox) –

+0

向此列表器添加轉換參數。像btn.setOnClickListener((OnClickListener)這樣); –

+0

通過使用btn.setOnClickListener((OnClickListener)this); 我的活動不幸停止 –

0

上午,

$result = mysql_query("SELECT role,email FROM table1 where Username='$username' and Password='$password'",$con); 

當我accesing使用HTTP請求時,它,如何在輸入字段中獲得此2數據(角色&電子郵件) ,,,

return sb.toString(); 

或者我必須將它存儲在字符串或其他東西...