2017-02-03 5 views
0

Является ли новый для android каким-либо предложением, где я ошибаюсь? Всего ManiActiviy.javaОшибка Android Метод getText должен вызываться из потока пользовательского интерфейса, в настоящее время выведенный поток - это рабочий

Ошибка «Метод GetText должен вызываться из потока пользовательского интерфейса, в данный момент выводится нить рабочий»

В этих 3 линии она дает вышеуказанную ошибку.

person = new Person(); 
person.setName(etName.getText().toString()); 
person.setCountry(etCountry.getText().toString()); 
person.setTwitter(etTwitter.getText().toString()); 

Код класса

import android.app.Activity; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

public class MainActivity extends Activity implements OnClickListener { 

    TextView tvIsConnected; 
    EditText etName, etCountry, etTwitter; 
    Button btnPost; 

    Person person; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

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

     // get reference to the views 
     tvIsConnected = (TextView) findViewById(R.id.tvIsConnected); 
     etName = (EditText) findViewById(R.id.etName); 
     etCountry = (EditText) findViewById(R.id.etCountry); 
     etTwitter = (EditText) findViewById(R.id.etTwitter); 
     btnPost = (Button) findViewById(R.id.btnPost); 

     // check if you are connected or not 
     if (isConnected()) { 
      tvIsConnected.setBackgroundColor(0xFF00CC00); 
      tvIsConnected.setText("You are conncted"); 
     } else { 
      tvIsConnected.setText("You are NOT conncted"); 
     } 

     // add click listener to Button "POST" 
     btnPost.setOnClickListener(this); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    public static String POST(String url, Person person) { 
     InputStream inputStream = null; 
     String result = ""; 
     try { 

      // 1. create HttpClient 
      HttpClient httpclient = new DefaultHttpClient(); 

      // 2. make POST request to the given URL 
      HttpPost httpPost = new HttpPost(url); 

      String json = ""; 

      // 3. build jsonObject 
      JSONObject jsonObject = new JSONObject(); 
      jsonObject.accumulate("name", person.getName()); 
      jsonObject.accumulate("country", person.getCountry()); 
      jsonObject.accumulate("twitter", person.getTwitter()); 

      // 4. convert JSONObject to JSON to String 
      json = jsonObject.toString(); 

      // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
      // ObjectMapper mapper = new ObjectMapper(); 
      // json = mapper.writeValueAsString(person); 

      // 5. set json to StringEntity 
      StringEntity se = new StringEntity(json); 

      // 6. set httpPost Entity 
      httpPost.setEntity(se); 

      // 7. Set some headers to inform server about the type of the content 
      httpPost.setHeader("Accept", "application/json"); 
      httpPost.setHeader("Content-type", "application/json"); 

      // 8. Execute POST request to the given URL 
      HttpResponse httpResponse = httpclient.execute(httpPost); 

      // 9. receive response as inputStream 
      inputStream = httpResponse.getEntity().getContent(); 

      // 10. convert inputstream to string 
      if (inputStream != null) 
       result = convertInputStreamToString(inputStream); 
      else 
       result = "Did not work!"; 

     } catch (Exception e) { 
      Log.d("InputStream", e.getLocalizedMessage()); 
     } 

     // 11. return result 
     return result; 
    } 

    public boolean isConnected() { 
     ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
     if (networkInfo != null && networkInfo.isConnected()) 
      return true; 
     else 
      return false; 
    } 

    @Override 
    public void onClick(View view) { 

     switch (view.getId()) { 
      case R.id.btnPost: 
       if (!validate()) 
        Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show(); 
       // call AsynTask to perform network operation on separate thread 
       new HttpAsyncTask().execute("http://hmkcode.appspot.com/jsonservlet"); 
       break; 
     } 

    } 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    public Action getIndexApiAction() { 
     Thing object = new Thing.Builder() 
       .setName("Main Page") // TODO: Define a title for the content shown. 
       // TODO: Make sure this auto-generated URL is correct. 
       .setUrl(Uri.parse("http:// http://192.168.0.51:1337/sensormessage/pushSensorData?MAC=18:fe:34:a5:df:8c&OC=0&ALS=3 &POW=100")) 
       .build(); 
     return new Action.Builder(Action.TYPE_VIEW) 
       .setObject(object) 
       .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
       .build(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     AppIndex.AppIndexApi.start(client, getIndexApiAction()); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     AppIndex.AppIndexApi.end(client, getIndexApiAction()); 
     client.disconnect(); 
    } 

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

      person = new Person(); 
      person.setName(etName.getText().toString()); 
      person.setCountry(etCountry.getText().toString()); 
      person.setTwitter(etTwitter.getText().toString()); 
      return POST(urls[0], person); 

     } 

     // onPostExecute displays the results of the AsyncTask. 
     @Override 
     protected void onPostExecute(String result) { 
      Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show(); 
     } 
    } 

    private boolean validate() { 
     if (etName.getText().toString().trim().equals("")) 
      return false; 
     else if (etCountry.getText().toString().trim().equals("")) 
      return false; 
     else if (etTwitter.getText().toString().trim().equals("")) 
      return false; 
     else 
      return true; 
    } 

    private static String convertInputStreamToString(InputStream inputStream) throws IOException { 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
     String line = ""; 
     String result = ""; 
     while ((line = bufferedReader.readLine()) != null) 
      result += line; 

     inputStream.close(); 
     return result; 

    } 
} 

И Person.java

package android.hmkcode.com.android_post_json; 

/** 
* Created by HP on 2/2/2017. 
*/ 

public class Person { 

    private String name; 
    private String country; 
    private String twitter; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getCountry() { 
     return country; 
    } 

    public void setCountry(String country) { 
     this.country = country; 
    } 

    public String getTwitter() { 
     return twitter; 
    } 

    public void setTwitter(String twitter) { 
     this.twitter = twitter; 
    } 
} 
+0

Рассмотрите возможность получения ответа, если ему помогли !!! – W4R10CK

ответ

0

Попытка передать EditText переменные с конструктором класса HttpAsyncTask как:

private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
    EditText etName; 
    EditText etCountry; 
    EditText etTwitter; 
    Context context; 

    //Constructor to get the values 
    public HttpAsyncTask(EditText etName, EditText etCountry, EditText etTwitter, Context context){ 
     this.etName = etName; 
     this.etCountry = etCountry; 
     this.etTwitter = etTwitter; 
     this.context = context; 
    } 

    @Override 
    protected String doInBackground(String... urls) { 

     person = new Person(); 
     person.setName(etName.getText().toString()); 
     person.setCountry(etCountry.getText().toString()); 
     person.setTwitter(etTwitter.getText().toString()); 
     return POST(urls[0], person); 

    } 

    // onPostExecute displays the results of the AsyncTask. 
    @Override 
    protected void onPostExecute(String result) { 
     Toast.makeText(context, "Data Sent!", Toast.LENGTH_LONG).show(); 
    } 
} 

A й передать editext переменные внутри как:

new HttpAsyncTask(etName, etCountry, etTwitter, MainActivity.this).execute("http://hmkcode.appspot.com/jsonservlet"); 
0

Вы не можете открыть представление в фоновом потоке (рабочий поток)

Вы можете изменить свой код, как этот

private class HttpAsyncTask extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... urls) { 

      person = new Person(); 
      person.setName(urls[1]); //name 
      person.setCountry(urls[2]); //country 
      person.setTwitter(urls[3]); // twitter 
      return POST(urls[0], person); 

     } 

     // onPostExecute displays the results of the AsyncTask. 
     @Override 
     protected void onPostExecute(String result) { 
      Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show(); 
     } 
    } 

и называют, как это

new HttpAsyncTask().execute("http://hmkcode.appspot.com/jsonservlet",etName.getText().toString(),etCountry.getText().toString(),etTwitter.getText().toString()); 
-1

Ваш пользователь не может получить доступ к компоненту пользовательского интерфейса в рабочем потоке (так как doInBackground создает ker thread), если вы не используете Looper.
Решение 1: Внутри метод onPreExecute() вы можете получить доступ к компонентам ui при запуске в самой пользовательской строке. Выполните следующие действия

private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
    Person person; 

    @Override 
    protected void onPreExecute(){ 
     person = new Person(); 
     person.setName(etName.getText().toString()); 
     person.setCountry(etCountry.getText().toString()); 
     person.setTwitter(etTwitter.getText().toString()); 
    } 

    @Override 
    protected String doInBackground(String... urls) { 
     return POST(urls[0], person); 

    } 

Решение 2 Просто создайте человек после OnClick и передать объект человека в конструкторе AsynTask()

@Override 
    public void onClick(View view) { 

     switch (view.getId()) { 
      case R.id.btnPost: 
       if (!validate()) 
        Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show(); 
       person = new Person(); 
       person.setName(etName.getText().toString()); 
       person.setCountry(etCountry.getText().toString()); 
       person.setTwitter(etTwitter.getText().toString()); 
       // call AsynTask to perform network operation on separate thread 
       new HttpAsyncTask(person).execute("http://hmkcode.appspot.com/jsonservlet"); 
       break; 
     } 

    } 

И в конструкторе вам необходимо следующее.

private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
    Person person; 
    Context context; 

    //Constructor to get the values 
    public HttpAsyncTask(Person person, Context context){ 
     this.person= person; 
     this.context = context; 
    } 

    @Override 
    protected String doInBackground(String... urls) { 
     return POST(urls[0], person); 

    } 
+0

Если кто-то голосует, ответ ... по крайней мере, упоминайте причину в комментариях –

Смежные вопросы

 Смежные вопросы