2012-11-23 38 views
3

我在我的網站中使用wordpress。在我的一個xml文件用戶必須填寫內容像書價格等然後我想發佈數據從當用戶點擊發布按鈕時,android應用到網站。我怎樣才能做到這一點。如果您提供有關這方面的任何信息,將會非常有幫助。下面是我的代碼如何從APP發佈到網站當點擊發布用戶點擊發布按鈕

public class Post_Ads extends Activity { 
private static final int SELECT_PHOTO = 100; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_post__ads); 

    // do action when click on browse button 
    Button button = (Button) findViewById(R.id.browse_btn); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
      Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
      photoPickerIntent.setType("image/*"); 
      startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

     } 
    }); 
    Button btnpost = (Button) findViewById(R.id.post_btn); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 


     } 
    }); 

} 
// select photo from phone gallery 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO: 
     if(resultCode == RESULT_OK){ 
      Uri selectedImage = imageReturnedIntent.getData(); 
      InputStream imageStream; 
      try { 
       imageStream = getContentResolver().openInputStream(selectedImage); 
       Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    } 

} 
public void postData(String title, String price, String city, String state, String country,String zip, String description, String image) { 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("site url"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     nameValuePairs.add(new BasicNameValuePair("Title", title)); 
     nameValuePairs.add(new BasicNameValuePair("price", price)); 
     nameValuePairs.add(new BasicNameValuePair("City", city)); 
     nameValuePairs.add(new BasicNameValuePair("State", state)); 
     nameValuePairs.add(new BasicNameValuePair("Country", country)); 
     nameValuePairs.add(new BasicNameValuePair("Zip/Postal Code", zip)); 
     nameValuePairs.add(new BasicNameValuePair("Description", description)); 
     nameValuePairs.add(new BasicNameValuePair("Image", image)); 
     // 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 
    } 

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


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 

      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

activity_post_ads.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="right" 
android:orientation="vertical" > 


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top|left" 
    android:text="@string/PostYourAds" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" /> 
    <LinearLayout android:layout_width="wrap_content"  android:layout_height="fill_parent" > 
<TextView 
android:id="@+id/title" 
android:layout_width="70dp" 
android:layout_height="wrap_content" 
android:text="@string/Title" /> 
    <EditText 
android:id="@+id/title1" 
android:layout_width="210dp" 
android:layout_height="wrap_content" 
android:gravity="left" 
android:hint="@string/none" > 
    <requestFocus /> 
    </EditText> 
     </LinearLayout> 
    <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" > 
    <TextView 
     android:id="@+id/price" 
     android:layout_width="67dp" 
     android:layout_height="wrap_content" 
     android:text="@string/price" /> 
    <EditText 
android:id="@+id/price1" 
android:layout_width="210dp" 
android:layout_height="wrap_content" 
android:ems="10" 
android:hint="@string/none" 
android:inputType="number" > 
    <requestFocus /> 
</EditText> 
    </LinearLayout> 

<requestFocus /> 
</EditText> 
</LinearLayout> 

    <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" > 
<TextView 
android:id="@+id/state" 
android:layout_width="68dp" 
android:layout_height="wrap_content" 
android:text="@string/state" /> 

<EditText 
android:id="@+id/state1" 
android:layout_width="210dp" 
android:layout_height="wrap_content" 
android:ems="10" 
android:hint="@string/none" > 

    <requestFocus /> 
    </EditText> 
    </LinearLayout> 

<LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" > 
    <TextView 
     android:id="@+id/zip" 
     android:layout_width="73dp" 
     android:layout_height="match_parent" 
     android:text="@string/zip" /> 
<EditText 
android:id="@+id/zip1" 
android:layout_width="210dp" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:ems="10" 
android:inputType="number" 
android:hint="@string/none"> 

<requestFocus /> 
</EditText> 
    </LinearLayout> 

<LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" > 
<TextView 
android:id="@+id/description" 
android:layout_width="88dp" 
android:layout_height="30dp" 
android:text="@string/description" /> 
<EditText 
    android:id="@+id/editText1" 
    android:layout_width="210dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:ems="10" 
    android:hint="@string/none" 
    android:inputType="textMultiLine" /> 
    </LinearLayout> 

<LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" > 
    <TextView 
     android:id="@+id/Image" 
     android:layout_width="73dp" 
     android:layout_height="33dp" 
     android:text="@string/image" /> 
<EditText 
android:id="@+id/image1" 
android:layout_width="134dp" 
android:layout_height="wrap_content" 
android:ems="10" 
android:hint="@string/none" > 

<requestFocus /> 
</EditText> 

<Button 
android:id="@+id/browse_btn" 
style="?android:attr/buttonStyleSmall" 
android:layout_width="71dp" 
android:layout_height="wrap_content" 
android:text="@string/browse_btn" /> 
</LinearLayout> 

<Button 
    android:id="@+id/post_btn" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="@string/post" 
    android:onClick="postData()" /> 

</LinearLayout> 

回答

1

你shold瞭解HTTP類第一。從Android應用程序向服務器發佈數據的示例代碼如下所示。

public void postData(String bookname, String price) { 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("your site url"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     nameValuePairs.add(new BasicNameValuePair("book", bookname)); 
     nameValuePairs.add(new BasicNameValuePair("price", price)); 
     // 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 
    } 

} 

所以List<NameValuePair> nameValuePairs將要舉辦的是應通過POST方法去服務器的數據。傳遞下面的值。

nameValuePairs.add(new BasicNameValuePair("book", bookname)); 
nameValuePairs.add(new BasicNameValuePair("price", price)); 

基本上當一個按鈕被點擊如下圖所示上面的函數應該叫,

 postbutton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) 
     { 
      // call postData here. 
     } 
    }); 

確保你在你的網站來處理這些請求設置的所有事情。請注意,對於一些較新或最近的Android版本,您需要有AsyncTask來執行網絡操作。

編輯:將您的oncreate方法更改爲以下代碼。你還沒有調用postData(....)方法,這就是爲什麼你沒有得到。還有一件事,要上傳文件到服務器,你需要有不同的代碼看起來here你會得到一些想法。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_post__ads); 

    // do action when click on browse button 
    Button button = (Button) findViewById(R.id.browse_btn); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
      Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
      photoPickerIntent.setType("image/*"); 
      startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

     } 
    }); 
    Button btnpost = (Button) findViewById(R.id.post_btn); 
    btnpost.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
      postData("title","price", "city", "state","country","zip", "description",  "image");  

     } 
    }); 

} 
+0

當我點擊後按鈕它沒有做任何事情,我不能夠看到我的網站上發佈的任何內容通過點擊在Android應用 – SRY

+0

發佈我需要看到你的android代碼,我需要知道你在你的網站上設置的東西。而且,你是怎麼稱呼AsyncTask發生的? – Vinay

+0

我已經更新了代碼,在我的網站上,用戶可以通過填寫標題,價格,城市,州等內容來發布廣告,他們需要從手機上傳圖片,然後當他們點擊帖子時,該帖子應該出現在我的網站 – SRY

0

您可以通過android的webservice調用將值發送到wbsite。使用POST或GET方法。 下面是HttpPost方法發送數據的代碼,

String url    = "your_webservice_URL"; 

try 
{ 
    HttpPost loginHttpPost = new HttpPost(url); 
    HttpContext localContext = new BasicHttpContext();   

    MultipartEntity multipartContent = new MultipartEntity(); 
    multipartContent.addPart("parameter1", new StringBody(value1)); 
    multipartContent.addPart("parameter2", new StringBody(value2)); 
    loginHttpPost.setEntity(multipartContent); 

    HttpClient objHttpClient = new DefaultHttpClient(); 
    HttpResponse response = objHttpClient.execute(loginHttpPost,localContext); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
} 

下面是HTTPGET方法來發送數據的代碼,

String webserviceurl     = "your_webservice_URL"; 

List<NameValuePair> lstAddToken   = new ArrayList<NameValuePair>(); 
lstAddToken.add(new BasicNameValuePair("parameter1",value1)); 
lstAddToken.add(new BasicNameValuePair("parameter2",value2)); 

//add parameters to the URL 
webserviceurl       += "?"; 
String paramString      = URLEncodedUtils.format(lstAddToken, "utf-8"); 
webserviceurl       += paramString; 

//Call the webservice using HttpGet with parameters and get the response from webservice 
try 
{ 
    HttpGet loginHttpget  = new HttpGet(webserviceurl); 
    HttpClient objHttpClient = new DefaultHttpClient(); 
    HttpResponse response = objHttpClient.execute(loginHttpget); 
    HttpEntity entity = response.getEntity(); 
    is   = entity.getContent(); 
} 
catch (Throwable t) 
{ 
    Log.e("log_tag", "Error converting result "+t.toString()); 
}