Android オブジェクトで作成したオブジェクトを使用して、json として値を PHP Web サービスに送信しています。送信はうまくいきます。しかし、送信後、私のデザインに基づいて Web サービスからオブジェクトを収集する必要があり、これをテストして、投稿が成功したことを確認します。しかし、キャッチ ステートメントを介して例外として Log cat に面白いメッセージが表示されます。私は、jsonをphp WebサービスからAndroid Javaコードに圧縮する方法に関係していると信じています。
BufferedReader コンストラクターで使用されるデフォルト バッファー サイズ以下の例外 。8k 文字のバッファが必要な場合は、明示することをお勧めします。
JSON OUTPUT 11-15 07:48:02.622: I/global(276): BufferedReader コンストラクターで使用されるデフォルトのバッファー サイズ。8k 文字のバッファが必要な場合は、明示することをお勧めします。11-15 07:48:02.622: I/output(276):
11-15 07:48:02.622: I/output(276):警告: ヘッダー情報を変更できません - ヘッダーは既に送信されています (出力は C:\ で開始されました) wamp\www\Rhema\config\config.php:17) のC:\wamp\www\Rhema\webservice\RegisterMember.php行50
11-15 07:48:02.622: I/output(276): {"名前":"femi","ユーザー名":"dsasdfasft","電話":"456346345345645","メール":"a@yahoo.com","送信":"送信"}
My java code below doing the sending
//The first code class is used to represent the json object that would be passed to php
package com.example.objects;
public class MemberModel {
private String Name;
private String Username;
private String Phone;
private String Email;
private String Submit;
public void setName(String Name){
this.Name = Name;
}
public String getName(){
return Name;
}
public void setUsername(String Username){
this.Username = Username;
}
public String getUsername(){
return Username;
}
public void setPhone(String Phone){
this.Phone = Phone;
}
public String getPhone(){
return Phone;
}
public void setEmail(String Email){
this.Email = Email;
}
public String getEmail(){
return Email;
}
public void setSubmit(String Submit){
this.Submit = Submit;
}
public String getSubmit(){
return Submit;
}
//This is going to be used to set the json string notation
public final static String Member_Name = "Name";
public final static String Member_Username = "Username";
public final static String Member_Phone = "Phone";
public final static String Member_Email = "Email";
public final static String Member_Submit = "Submit";
}
//This is a snippet from the asynctask class that is sending the json object to php
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try{
JSONHttpClient jsonobject = new JSONHttpClient();
model = (MemberModel)jsonobject.PostObject(RestfulUrl.RegisterURL, model, MemberModel.class);
Log.i("gothere","here");
if(model != null){
Log.i("return",model.getSubmit());
}
else{
Log.i("return","wrong values");
}
}
catch(Exception e){
}
return null;
}
//The snippet below is the main part of the json object that does the post
//I have tested this with a asp.net mvc app server and it works prity well
public <T> T PostObject(final String url, final T object, final Class<T> objectClass) {
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
StringEntity stringEntity = new StringEntity(new GsonBuilder().create().toJson(object));
Log.i("jsonobject",stringEntity.toString());
httpPost.setEntity(stringEntity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Accept-Encoding", "gzip");
HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
InputStream inputStream = httpEntity.getContent();
Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);
}
String resultString = convertStreamToString(inputStream);
inputStream.close();
return new GsonBuilder().create().fromJson(resultString, objectClass);
}
} catch (UnsupportedEncodingException e) {
Log.i("a",e.toString());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ClientProtocolException e) {
Log.i("b",e.toString());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
Log.i("c",e.toString());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
public <T> T PostParams(String url, final List<NameValuePair> params, final Class<T> objectClass) {
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
return PostObject(url, null, objectClass);
}
private String convertStreamToString(InputStream inputStream) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
} catch (IOException e) {
Log.i("first",e.toString());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
inputStream.close();
} catch (IOException e) {
Log.i("second",e.toString());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
return stringBuilder.toString();
}
//My php code below
function __construct(){
$this->connection = new Connector();
$this->connection->doConnection();
$json = file_get_contents('php://input');
$obj = json_decode($json);
if(isset($obj))
$this->register($obj);
}
function register($obj){
$name = $obj->{"Name"};
$username = $obj->{"Username"};
$phone = $obj->{"Phone"};
$email = $obj->{"Email"};
$query = "insert into member (name, username, phone, email,rhemabranchid ) values ('$name','$username','$phone','$email',1)";
$result = mysql_query($query) or die(mysql_error());
$id = mysql_insert_id();
mysql_close();
if($id >0){
// echo "successful";
$array = array("Name"=>$name,"Username"=>$username,"Phone"=>$phone,"Email"=>$email,"Submit"=>"Submit");
header('Content-type: application/json');
echo json_encode($array);
}
else
echo "failed";
}