2017-12-08 2 views
0

주요 활동의 추상 메소드 'doInBackground (에 Params ...)'구현해야합니다클래스 'SigninActivity은'하나 추상적 인 선언 또는 'AsyncTask를'

package com.example.phpmysql; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private EditText usernameField,passwordField; 
    private TextView status,role,method; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     usernameField = (EditText)findViewById(R.id.editText1); 
     passwordField = (EditText)findViewById(R.id.editText2); 

     status = (TextView)findViewById(R.id.textView6); 
     role = (TextView)findViewById(R.id.textView7); 
     method = (TextView)findViewById(R.id.textView9); 
    } 



    public void login(View view){ 
     String username = usernameField.getText().toString(); 
     String password = passwordField.getText().toString(); 
     method.setText("Get Method"); 
     new SigninActivity(this,status,role,0).execute(username,password); 

    } 

    public void loginPost(View view){ 
     String username = usernameField.getText().toString(); 
     String password = passwordField.getText().toString(); 
     method.setText("Post Method"); 
     new SigninActivity(this,status,role,1).execute(username,password); 
    } 
} 

SigninActivity :

package com.example.phpmysql; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.URI; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 

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 SigninActivity extends AsyncTask{ 



    private TextView statusField,roleField; 
    private Context context; 
    private int byGetOrPost = 0; 

    //flag 0 means get and 1 means post.(By default it is get.) 

    public SigninActivity(Context context,TextView statusField,TextView 
     roleField,int flag) { 
     this.context = context; 
     this.statusField = statusField; 
     this.roleField = roleField; 
     byGetOrPost = flag; 
    } 

    protected void onPreExecute(){ 
    } 

    @Override 
    protected String doInBackground(String... arg0) { 
     if(byGetOrPost == 0){ //means by Get Method 

      try{ 
       String username = (String)arg0[0]; 
       String password = (String)arg0[1]; 
       String link = "http://myphpmysqlweb.hostei.com/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()); 
      } 
     } else{ 
      try{ 
       String username = (String)arg0[0]; 
       String password = (String)arg0[1]; 

       String link="http://myphpmysqlweb.hostei.com/loginpost.php"; 
       String data = URLEncoder.encode("username", "UTF-8") + "=" + 
         URLEncoder.encode(username, "UTF-8"); 
       data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + 
         URLEncoder.encode(password, "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())); 

       StringBuilder sb = new StringBuilder(); 
       String line = null; 

       // Read Server Response 
       while((line = reader.readLine()) != null) { 
        sb.append(line); 
        break; 
       } 

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

    @Override 
    protected void onPostExecute(String result){ 
     this.statusField.setText("Login Successful"); 
     this.roleField.setText(result); 
    } 
} 

public class SigninActivity extends AsyncTask을 클래스 SigninActivityabstract으로 선언되어야하거나 의 추상 메소드를 AsyncTask에 구현해야한다는 것을 나타내는 빨간색 밑줄로 표시되어 있습니까?

doInBackground를 추가하려고했지만 사용하지 않는다고 표시되어 있습니까? 내가 그것을 adbstract 만드는 시도하지만 난 당신이

public class SigninActivity extends AsyncTask<String, Void, String> 

로 일반 매개 변수를 제공해야 mainactivity

+1

https://developer.android.com/reference/android/os/AsyncTask.html 당신은 당신의 작업에서'execute()'를 호출하지 않습니다. – jdv

+1

- asyncTask 클래스를 입력해야합니다. (공용 클래스 SigninActivity는 AsyncTask 을 확장합니다.) - AsyncTask 클래스 SigninActivity의 이름을 지정하지 않는 것이 좋습니다. 액티비티는 안드로이드 프레임 워크 구성 요소이며 그 이름은 매우 오도 된 것입니다 .. – RScottCarson

답변

2

에서 클래스를 호출 할 수있는 다른 매개 변수 유형은 원시 형 및 서명되지 않습니다 것입니다 일치

+1

. AsyncTask를 확장하는 클래스는 Activity라고 불려서는 안되며, 혼동 스럽습니다. – donfuxx

+0

@donfuxx 예, 완전히 동의합니다. 여러분의 노력과 가치있는 의견에 감사드립니다. :) –

관련 문제