0

Hey guys i have this problem. i dont know what happened but its the first time i see this error. i used this code and it never cause any problem. i have no clue whats the problem becuase that code was working with no errors and suddenly it gives me that error and close the application.

The Code

    public void run() {
    try {
        send();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}



public void send() throws ClientProtocolException, IOException
{
    OutputStream mOutputStream = null;
    BufferedWriter mWriter = null;

    List<NameValuePair> mParameters = req.getParameters();

    URL url = null;
    HttpURLConnection connection = null;
    try {
        Looper.prepare();
        url = new URL(req.returnRequestUrl());
        connection = (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(READ_TIMEOUT);
        connection.setConnectTimeout(CONNECTION_TINEOUT);
        connection.setRequestMethod(Params.HTTP_REQUEST_METHOD_POST);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        mOutputStream = connection.getOutputStream();
        mWriter = new BufferedWriter(new OutputStreamWriter(mOutputStream, Params.UTF8));
        String sparams = URLEncodedUtils.format(mParameters, Params.UTF8);
        mWriter.write(sparams);
        mWriter.flush();

        mResponseCode = connection.getResponseCode();
        if (mResponseCode > 203) {
            mData = readWebData(connection.getErrorStream());
            this.req.getResponse().notGoodServerEroorr();
        } else {
            mData = readWebData(connection.getInputStream());

        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                if (mOutputStream != null)
                    mOutputStream.close();
                if (mWriter != null)
                    mWriter.close();

            } catch (IOException e) {
                 // TODO Auto-generated catch block
                e.printStackTrace();
            }

            connection.disconnect();
            evaluateDataAndRespondToFragment(mData);
            myLooper = Looper.myLooper();
            Looper.loop();
            myLooper.quit();
        }

    }
}

private void evaluateDataAndRespondToFragment(String mData) {
    Listen lis = this.req.getResponse();
    if (mData.equals("1"))
        lis.good();
    else
        lis.notGood();
}
Bolandian Eran
  • 211
  • 1
  • 4
  • 21

3 Answers3

0

Create a handler and call it from the new thread to update views

final Hanlder hanlder=new Handler(){
    @Override
    public void handleMessage(Message msg){
         //update views here
    }
}
//from the thread
new Thread(new Runnable{
    @Override
    public void run(){
       //do your async task
       //call hanlder
       handler.obtainMessage().sendToTarget()
    }
}).start();

Refer here to select in between runOnUiThread and handler. https://stackoverflow.com/a/7452913/5819589

Community
  • 1
  • 1
Malith Lakshan
  • 762
  • 6
  • 12
0

This problem is due to connection request. Generally these types of request does not work on main thread. So, create a new thread to run this code or use runOnUiThread() method. This may solve your problem. try this

    public void run() {
    runOnUiThread(new Runnable() {
            @Override
            public void run() {
                send();
            }
        });
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
UserSharma
  • 458
  • 6
  • 20
0

As Error Message Describes, Only Main Thread Can Touch VAny Kind of Views, It seems

evaluateDataAndRespondToFragment(mData);

above method touches Main Thread Components which is not allowed by Android, Call this method from Main Thread Like this -

getActivity().runOnUiThread(new Runnable() {
   @Override
   public void run() {
     evaluateDataAndRespondToFragment(mData);
   }
});
Malith Lakshan
  • 762
  • 6
  • 12
AmniX
  • 653
  • 5
  • 12
  • So i need to add the method "evaluateDataAndRespondToFragment(mData)" to the public void run() method and remove it from the send() method? – Bolandian Eran May 24 '16 at 10:38