Что меня привело сюда:
Мне нужно получить данные со транички на которое есть только лишь одна строка (результат из базы) при переходе на страничку из браузера http://мойсайт.ком/test.php я вижу полученый результат из базы....
теперь мне нужно этот текст вывести в TextView.... что я для этого делаю:
создаю новый проект File -> new -> Android Application Project (Eclipse)
ну там обзываю к примеру http и т.п.
далее что я делаю:
в layout/activivti_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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="178dp"
android:layout_marginTop="214dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Код: Выделить всё
package com.example.http;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
InputStream mInputStream = null;
try {
DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpPost mHttpPost = new HttpPost("http://software44.ru/android.php");
HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
HttpEntity mHttpEntity = mHttpResponse.getEntity();
mInputStream = mHttpEntity.getContent();
BufferedReader mReader = new BufferedReader(new InputStreamReader( mInputStream, "UTF-8"), 8); //что6ы не 6ыло крякозя6ров
StringBuilder mStringBuilder = new StringBuilder();
String line = null;
while ((line = mReader.readLine()) != null)
{
mStringBuilder.append(line + "\n");
}
mInputStream.close();
tv.setText(mStringBuilder.toString());
}
catch (Exception e)
{
tv.setText(e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Помогите люди добрые новичку....