我有一个应用程序,我需要从我的数据库接收图像(在有人询问之前,是的,它需要来自数据库)。
在我的PHP文件中,我发送完整的完整字符串,但在Android中我只收到一半的字符串左右
你们有没有提示为什么会发生这种情况?
任何人都可以帮助我吗?
代码
ServerRequest.java:
public void FetchServicoFotoDataInBackground(int CodServico, GetServicoFotoCallBack userCallback) {
new FetchServicoFotoDataAsyncTasck(CodServico, userCallback).execute();
}
public class FetchServicoFotoDataAsyncTasck extends AsyncTask<Void, Void, ArrayList<String>> {
ArrayList<String> ltservico;
int CodServico;
GetServicoFotoCallBack servCallback;
public FetchServicoFotoDataAsyncTasck(int CodServico, GetServicoFotoCallBack servicoCallback) {
this.CodServico = CodServico;
this.servCallback = servicoCallback;
}
@Override
protected ArrayList<String> doInBackground(Void... params) {
ArrayList<String> returnedServico = null;
try {
URL url = new URL(SERVER_ADDRESS + "myphpfile.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("CodServico", this.CodServico+"");
final String postParameters = builder.build().getEncodedQuery();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(postParameters.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoInput(true);
conn.setDoOutput(true);
//send the POST out
PrintWriter pw = new PrintWriter(conn.getOutputStream());
pw.print(postParameters);
pw.close();
conn.connect();
String result = convertStreamToString(conn.getInputStream());
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
returnedServico = new ArrayList<>();
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
String imagem = json_data.getString("FotoServico");
returnedServico.add(imagem);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Exception", "Erro[" + e.getMessage() + "] ");
}
return returnedServico;
}
@Override
protected void onPostExecute(ArrayList<String> returnedServico) {
servCallback.done(returnedServico);
super.onPostExecute(ltservico);
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
MyActivity.java:
public class verfotoserv extends BaseNavegationActivity {
private LinearLayout lnrVerImages;
private ImageView imageView;
int codservico=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verfotoserv);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
lnrVerImages = (LinearLayout) findViewById(R.id.lnrImages);
final Bundle extras = getIntent().getExtras();
if (extras != null) {
codservico = extras.getInt("CodServico");
}
ServerRequests serverRequests = new ServerRequests(this);
serverRequests.FetchServicoFotoDataInBackground(codservico, new GetServicoFotoCallBack() {
@Override
public void done(ArrayList<String> returnImagens) {
try {
if (returnImagens == null) {
throw new Exception("Não existem dados ou ocorreu um erro no servidor\nTente novamente mais tarde.");
}
for (String imagem : returnImagens) {
Log.i("ImagemRecebida",imagem);
byte[] imageAsBytes = Base64.decode(imagem.getBytes(), Base64.DEFAULT);
imageView = new ImageView(verfotoserv.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
imageView.setLayoutParams(params);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes,0,imageAsBytes.length));
imageView.setAdjustViewBounds(true);
lnrVerImages.addView(imageView);
}
}
catch (Exception erro){
erro.printStackTrace();
showError(erro);
}
}
});
}
private void showError(Exception erro){
Log.e("Erro", erro.getMessage());
AlertDialog.Builder dialogBuilder=new android.app.AlertDialog.Builder(verfotoserv.this);
dialogBuilder.setMessage("Erro:"+erro.getMessage());
dialogBuilder.setPositiveButton("Ok", null);
dialogBuilder.show();
}
}
MyActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/lnrVerImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
Myphpfile:
<?php
include_once "ligarbd.php";
$codservico = $_POST['CodServico'];
$SQL = "select * from table where CodServico=".$codservico;
$result = mysql_query($SQL);
$imagens = array();
$i = 0;
while ( $postData = mysql_fetch_assoc($result) ) {
$imagens[$i][FotoServico]=base64_encode($postData['FotoServico']);
$i = $i + 1;
}
echo json_encode($imagens);
?>
答案 0 :(得分:0)
我错了它收到改变这个所需的一切
lnrVerImages = (LinearLayout) findViewById(R.id.lnrImages);
到此
lnrVerImages = (LinearLayout) findViewById(R.id.lnrVerImages);