Reputation: 43
Good, not much English, but try to be as clear as possible.
I am very newbie in java. I started a few days ago. I'm trying to develop a reading of a website a arhico php. But it does not work. Always returns me the catch
I think we should do something with Thread, but i do not know how.
I'm using Android studio.
I expose the data that I have.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tuto1.app" >
<uses-sdk
android:minSdkVersion="8"
android:maxSdkVersion="15"/>
<uses-permission android:name="android.permission.INTERNETr"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.tuto1.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.tuto1.app.MainActivity">
<TextView
android:text="@string/hello_world"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#044234"
android:textColor="#ffff"
android:textSize="30sp"
android:id="@+id/text1" />
MainActivity.java
package com.example.tuto1.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpHandler handler = new httpHandler();
String txt = handler.post("http://demo.com/demo.php");
TextView t = (TextView)findViewById(R.id.text1);
t.setText(txt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
httpHander.java (Class)
package com.example.tuto1.app;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class httpHandler {
public String post(String posturl){
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(posturl);
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
String text = EntityUtils.toString(ent);
return text;
}
catch (Exception e) { return "mi error"; }
}
}
updated The solution is this.
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
class TheTask extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... arg0) {
String text =null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(arg0[0]);
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
text = EntityUtils.toString(ent);
}
catch (Exception e)
{
e.printStackTrace();
}
return text;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
TextView t = (TextView)findViewById(R.id.text1);
t.setText(result);
}
}
new TheTask().execute("http://demo.com/php.php");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 3
Views: 11620
Reputation: 133560
You need to use a Thread
or Aysnctask
or a Volley
library. You cannot run network operation on the main UI thread.
HttpResponse resp = httpclient.execute(httppost); // must be in a thread.
You could create your own java threads but asynctask makes it easier. You will find information and example @
http://developer.android.com/reference/android/os/AsyncTask.html
Example:
public class MainActivity extends Activity {
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // activity main is the activity that you are currently working with
t = (TextView)findViewById(R.id.text1); // this is the text view where you want the text response to appear.
new TheTask().execute("http://demo.com/demo.php");
}
class TheTask extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... arg0) {
String text =null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(arg0[0]);
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
text = EntityUtils.toString(ent);
}
catch (Exception e)
{
e.printStackTrace();
}
return text;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
t.setText(result);
}
}
}
Upvotes: 1