import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.epapandroid.comm.CommonUtil;
import android.R.bool;
import android.R.integer;
import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.util.Base64;
public JSONArray GetUploadTaskInfo(Integer DAYRANGEID) {
JSONArray jsonArray = new JSONArray();
DayPlanDevStatueService dayPlanDevStatueService = new DayPlanDevStatueService(
m_context);
DefectInfoService defectInfoService = new DefectInfoService(m_context);
DefectPhotoService defectPhotoService = new DefectPhotoService(
m_context);
// 获取支柱状态。
Cursor cursor = dayPlanDevStatueService
.getCursorDevStatueByUp(DAYRANGEID);
try {
while (cursor.moveToNext()) {
JSONObject jsonObject = new JSONObject();
Object dObject = cursor.getString(cursor
.getColumnIndex("STATUE"));
Integer DEVICEID = cursor.getInt(cursor
.getColumnIndex("DEVICEID"));// 支柱ID。
jsonObject.put(
"DAYPLANDEVSTATUEID",
cursor.getInt(cursor
.getColumnIndex("DAYPLANDEVSTATUEID"))).put(
"STATUE", dObject);
// 获取缺陷信息
Cursor cursorDefect = defectInfoService
.getCursorDefectInfoByRangID(DAYRANGEID, DEVICEID);
// 获取缺陷状态。
Cursor cursorDefectPhoto = defectPhotoService
.getCursorDefectPhoto(DAYRANGEID, DEVICEID);
JSONArray jsonArrayDefect = new JSONArray();
while (cursorDefect.moveToNext()) {
/**
* 构造JSON缺陷记录
*/
JSONObject jsonObjectDefect = new JSONObject();
jsonObjectDefect.put("DEFECTINFOID",
cursorDefect.getInt(cursorDefect
.getColumnIndex("DEFECTINFOID")));
jsonObjectDefect.put("DAYRANGEID", cursorDefect
.getInt(cursorDefect.getColumnIndex("DAYRANGEID")));
jsonObjectDefect.put("SITEID", cursorDefect
.getInt(cursorDefect.getColumnIndex("SITEID")));
jsonObjectDefect.put("PDEVICEID", cursorDefect
.getInt(cursorDefect.getColumnIndex("PDEVICEID")));
jsonObjectDefect.put("DEVICEID", cursorDefect
.getInt(cursorDefect.getColumnIndex("DEVICEID")));
jsonObjectDefect.put("DEFECTTYPEID_LARGE", cursorDefect
.getInt(cursorDefect
.getColumnIndex("DEFECTTYPEID_LARGE")));
jsonObjectDefect.put("DEFECTTYPEID_MIDDLE", cursorDefect
.getInt(cursorDefect
.getColumnIndex("DEFECTTYPEID_MIDDLE")));
jsonObjectDefect.put("DEFECTTYPEID_SMALL", cursorDefect
.getInt(cursorDefect
.getColumnIndex("DEFECTTYPEID_SMALL")));
jsonObjectDefect.put("DESCR", cursorDefect
.getString(cursorDefect.getColumnIndex("DESCR")));
jsonObjectDefect.put("DEFECTREASONID_LARGE", cursorDefect
.getInt(cursorDefect
.getColumnIndex("DEFECTREASONID_LARGE")));
jsonObjectDefect.put("DEFECTREASONID_SMALL", cursorDefect
.getInt(cursorDefect
.getColumnIndex("DEFECTREASONID_SMALL")));
jsonObjectDefect.put("LONGITUDE", cursorDefect
.getLong(cursorDefect.getColumnIndex("LONGITUDE")));
jsonObjectDefect.put("LATITUDE", cursorDefect
.getLong(cursorDefect.getColumnIndex("LATITUDE")));
jsonObjectDefect.put("CREATEDATE", cursorDefect
.getString(cursorDefect
.getColumnIndex("CREATEDATE")));
jsonObjectDefect.put("STATUE", cursorDefect
.getInt(cursorDefect.getColumnIndex("STATUE")));
jsonObjectDefect.put("SOLVEMETHOD", cursorDefect
.getString(cursorDefect
.getColumnIndex("SOLVEMETHOD")));
JSONArray jsonArrayDefectPhoto = new JSONArray();
/***
* 构造缺陷图片JSON
*/
while (cursorDefectPhoto.moveToNext()) {
JSONObject jsonObjectDefectPhoto = new JSONObject();
jsonObjectDefectPhoto.put("FILENAME", cursorDefectPhoto
.getString(cursorDefectPhoto
.getColumnIndex("FILENAME")));
String FILEPATH = cursorDefectPhoto
.getString(cursorDefectPhoto
.getColumnIndex("FILEPATH"));
// 将图片转为数据流。
FileInputStream fis;
String uploadBuffer = "";
try {
fis = new FileInputStream(FILEPATH);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) >= 0) {
baos.write(buffer, 0, count);
}
uploadBuffer = new String(Base64.encode(
baos.toByteArray(), Base64.DEFAULT)); // 进行Base64编码
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
jsonObjectDefectPhoto.put("FILEBUFFER", uploadBuffer);
jsonArrayDefectPhoto.put(jsonObjectDefectPhoto);
}
jsonObjectDefect.put("DEFECTPHOTO", jsonArrayDefectPhoto);
jsonArrayDefect.put(jsonObjectDefect);
}
jsonArray.put(jsonObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
return jsonArray;
}
将json字符串解析成一个javaBean:
public class MainActivity extends Activity {
private Button startButton = null;
private String data = "{\"name\":\"zhangsan\",\"age\":20}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button)findViewById(R.id.button1);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Gson gson = new Gson();
User user = gson.fromJson(data, User.class);
System.out.println("name---->" + user.getName());
System.out.println("age----->" + user.getAge());
}
});
}
}
将json字符串解析成多个JavaBean对象:
public class MainActivity_2 extends Activity {
private Button startButton = null;
private String data = "[{\"name\":\"zhangsan\",\"age\":20},{\"name\":\"lisi\",\"age\":21}]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button)findViewById(R.id.button1);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Type type = new TypeToken<ArrayList<User>>(){}.getType();
Gson gson = new Gson();
ArrayList<User> list = gson.fromJson(data, type);
Iterator<User> iterator = list.iterator();
while (iterator.hasNext()) {
User user = iterator.next();
System.out.println("name--->" + user.getName());
System.out.println("age---->" + user.getAge());
}
}
});
}
}
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}