|
|
|
ผมเขียน Android Add Insert Save data to Server Database (Web Server) insert ERROR |
|
|
|
|
|
|
|
คือผม เขียนโคตจาก https://www.thaicreate.com/mobile/android-add-insert-save-data-web-server.html แต่เพิ่มข้อมูลไม่ได้ แอฟเด้งพร้อมฟ้อง error ภาพจาก debug ที่ error ครับ และโคตทั้งหมดครับ อยากทราบว่า error อะไรอ่ะครับ
ขอโทษด้วยถ้ารบกวนเกินไปครับ ขอบคุณครับ
saveADDData.php
<?php
$objConnect = mysql_connect("localhost","root","1234");
$objDB = mysql_select_db("androidphoto");
$strnamephoto = $_POST["snamephoto"];
$strSQL = "SELECT * FROM photo WHERE namephoto = '".$strnamephoto."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult) {
$arr['StatusID'] = "0";
$arr['Error'] = "Namephoto Exists!";
echo json_encode($arr);
exit();
}
$strSQL = "INSERT INTO member (namephoto) VALUES ('".$strnamephoto."')";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
$arr['StatusID'] = "0";
$arr['Error'] = "Cannot save data!";
}else {
$arr['StatusID'] = "1";
$arr['Error'] = "";
}
mysql_close($objConnect);
echo json_encode($arr);
?>
activity_insert_textphoto.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".InsertTextPhotoActivity" >
<EditText
android:id="@+id/txtNamephoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15" >
</EditText>
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtNamephoto"
android:layout_below="@+id/txtNamephoto"
android:text="Save" />
</RelativeLayout>
InsertTextPhotoActivity.java
package com.example.durable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
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 org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("NewApi")
public class InsertTextPhotoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert_text_photo);
// Show the Up button in the action bar.
setupActionBar();
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(SaveData()){
}
}
});
}
public boolean SaveData(){
final EditText txtNamephoto = (EditText)findViewById(R.id.txtNamephoto);
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("Error! ");
ad.setIcon(android.R.drawable.btn_star_big_on);
ad.setPositiveButton("Close", null);
if(txtNamephoto.getText().length() == 0) {
ad.setMessage("Please input [Namephoto] ");
ad.show();
txtNamephoto.requestFocus();
return false;
}
//http://127.1.0.1/androidphoto/saveADDData.php
String url = "//http://192.168.1.7/androidphoto/saveADDData.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("snamephoto", txtNamephoto.getText().toString()));
String resultServer = getHttpPost(url,params);
String strStatusID = "0";
String strError = "Unknow Status!";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(strStatusID.equals("0")) {
ad.setMessage(strError);
ad.show();
} else {
Toast.makeText(InsertTextPhotoActivity.this, "Save Data Successfully", Toast.LENGTH_SHORT).show();
txtNamephoto.setText("");
}
return true;
}
public String getHttpPost(String url,List<NameValuePair> params){
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {// Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.insert_text_photo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Tag : Mobile, MySQL, Android
|
|
|
|
|
|
Date :
2014-01-02 21:54:49 |
By :
khuttiyamanu |
View :
1252 |
Reply :
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมเพิ่มข้อมูลได้ครั้งเดียวอ่ะครับ ครั้งต่อไป ฟ้องว่า error Cannot save data! เหมือนเดิมอ่ะครับ
|
|
|
|
|
Date :
2014-01-03 13:22:48 |
By :
khuttiyamanu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|