2013-07-31 2 views
1

저는 android를 처음 접하고 내 Android app에서 localhost에 데이터를 게시하려고합니다. 오류를 표시하지 않고 항상 명령을 보냈지 만 파일을 쓰지 않고 있거나 데이터를 게시하지 않은 것일 수도 있습니다. 누구든지 문제를 발견 할 수 있습니까, 아니면 어떻게 해결할 수 있습니까?android app에 로컬 호스트에 데이터를 게시하십시오.

MainActivity.java

package com.example.register; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import android.app.Activity; 
import android.opengl.Visibility; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener{ 

private EditText value; 
private Button btn; 
private ProgressBar pb; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
value=(EditText)findViewById(R.id.editText1); 
btn=(Button)findViewById(R.id.button1); 
pb=(ProgressBar)findViewById(R.id.progressBar1); 
pb.setVisibility(View.GONE); 
btn.setOnClickListener(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
getMenuInflater().inflate(R.menu.main, menu); 
return true; 
} 

public void onClick(View v) { 
// TODO Auto-generated method stub 
if(value.getText().toString().length()<1){ 
// out of range 
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show(); 
}else{ 
pb.setVisibility(View.VISIBLE); 
new MyAsyncTask().execute(value.getText().toString()); 
} 

} 

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

@Override 
protected Double doInBackground(String... params) { 
// TODO Auto-generated method stub 
postData(params[0]); 
return null; 
} 

protected void onPostExecute(Double result){ 
pb.setVisibility(View.GONE); 
Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show(); 
} 
protected void onProgressUpdate(Integer... progress){ 
pb.setProgress(progress[0]); 
} 

public void postData(String valueIWantToSend) { 
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://localhost/text.php"); 

try { 
// Add your data 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend)); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
// TODO Auto-generated catch block 
} catch (IOException e) { 
// TODO Auto-generated catch block 
} 
} 

} 
} 

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" > 

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="20dp" 
android:text="Enter Something Below:" 
android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
android:id="@+id/editText1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/textView1" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="30dp" 
android:ems="10" 
android:hint="" 
> 

<requestFocus /> 
</EditText> 

<ProgressBar 
android:id="@+id/progressBar1" 
style="?android:attr/progressBarStyleLarge" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/editText1" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="24dp" /> 

<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/textView1" 
android:layout_alignRight="@+id/editText1" 
android:layout_below="@+id/progressBar1" 
android:layout_marginTop="24dp" 
android:text="Submit" /> 

</RelativeLayout> 
+0

인쇄 응답 문자열을? 거기에 뭐가 들었 니? – KOTIOS

답변

4

당신은 로컬 호스트 시스템과 연결 10.0.2.2 IP 주소를 사용해야합니다.

HttpPost httppost = new HttpPost("http://localhost/text.php"); 

HttpPost httppost = new HttpPost("http://10.0.2.2/text.php"); 
+0

나는 이것을 사용하고있다. 그러나 여전히 게시하고 있지 않다. 실제로 나는 안드로이드를 처음 사용하고있다. 그리고 지금은 개미 오류 onclick을 보여주지 않기 때문에, 문제를 점검 할 수있는 곳을 말해 줄 수있다. 단지 명령을 보낸다. –

+0

@RohitGoel Check 'HttpResponse response = httpclient.execute (httppost);에서 얻은 응답. –

+0

당신은 어떻게 대답을 인쇄 할 수 있는지 말해 주시겠습니까 –

2
  1. 를 사용하여 IP 주소 대신 로컬 호스트이어야한다. 예 :

    HttpPost httppost = new HttpPost("http://192.168.0.10/mypage.php"); 
    
  2. AndroidManifest.xml 예에 인터넷 권한을 추가

+0

localhost는 어떤 식으로'192.168.0.10'입니까? –

+1

localhost의 IP 주소는 내 세계에서'127.0.0.1'입니다 ... –

+0

@MdKamruzzaman : 당신은 진실에서 더 멀어 질 수 없습니다. 우선 MongoDB의 구속력있는 주소에 대한 질문입니다. 둘째, 컴퓨터의 다른 모든 IP는 변경 될 수 있지만 localhost 또는 127.0.0.1은 변경되지 않습니다. –

관련 문제