我正在尝试发送服务器json并获取json以返回在listview中显示。 listview由refreshViewModels()方法处理。我发送的json字符串在这行代码中:
outputJSONserv =“{\”to \“:\”broadcast \“,\”type \“:\”1 \“,\”payload \“:”+ outputJSON +“}”;其中outputJSON是一个json字符串。
当我发送这个字符串时,我确实在一部手机上找到了一个字符串而不是另一部手机。我在logcat中收到一条警告,说明这是一个错误,与我的输出json的SocketException有关。以下是两个用于显示正在发生的事情的logcats以及有关SocketException的详细信息:
来自phone1的Logcat:
I/ActivityManager( 110): Config changed: { scale=1.0 imsi=310/0 loc=en_US touch=3 keys=1/1/2 nav=1/1 orien=1 layout=18 uiMode=17 seq=31}
I/ActivityManager( 110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher }
W/InputManagerService( 110): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45ca49c8
I/ActivityManager( 110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.google.android.gm/.ConversationListActivityGmail bnds=[83,240][157,319] }
--------- beginning of /dev/log/main
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:{"from":"65.46.170.6:14842","type":11,"payload":{"message":""}}
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883144 bytes in 216ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 221ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 214ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at oorg.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
D/WindowManager( 110): Home Key Test : false : false
D/PhoneWindow( 423): couldn't save which view has focus because the focused view android.webkit.WebView@45affd60 has no id.
D/Gmail ( 423): com.google.android.gm.HtmlConversationActivity@45b240d0 onPause() finished
I/ActivityManager( 110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher }
W/InputManagerService( 110): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45e77378
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 309ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 82ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:{"from":"65.46.170.6:14842","type":11,"payload":{"message":""}}
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883144 bytes in 116ms
D/dalvikvm( 2263): GC_FOR_MALLOC freed 42 objects / 1252232 bytes in 95ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 215 objects / 2513520 bytes in 93ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 198): GC_EXTERNAL_ALLOC freed 3572 objects / 355992 bytes in 102ms
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 144 objects / 1883560 bytes in 616ms
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 26 objects / 626264 bytes in 121ms
I/ActivityManager( 110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.iconnexus.client/.SplashScreen }
D/dalvikvm( 2263): GC_FOR_MALLOC freed 22 objects / 1564120 bytes in 155ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:{"from":"65.46.170.6:14842","type":11,"payload":{"message":""}}
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 449 objects / 3462208 bytes in 89ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 195ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
I/ActivityManager( 110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.iconnexus.client/.SplashScreen }
D/dalvikvm( 2263): GC_FOR_MALLOC freed 135 objects / 1883144 bytes in 233ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:{"from":"65.46.170.6:14842","type":11,"payload":{"message":""}}
来自手机2的Logcat:
E/AndroidClient(25069): Sent dataOutputStream
E/AndroidClient(25069): Before initEventHandlers
E/AndroidClient(25069): After initEventHandlers
E/AndroidClient(25069): Start Repeat Timer
E/AndroidClient(25069): In RepeatingTask()
E/AndroidClient(25069): Before inputJSON String
E/AndroidClient(25069): Hello String:org.apache.harmony.luni.net.SocketInputStream@45a15fb8
E/AndroidClient(25069): Hello String2:
D/dalvikvm(25069): GC_FOR_MALLOC freed 174 objects / 809424 bytes in 94ms
W/System.err(25069): java.net.SocketException: Socket is closed
W/System.err(25069): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err(25069): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err(25069): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err(25069): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err(25069): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON3:
I/ActivityManager( 110): Displayed activity com.iconnexus.client/.AndroidClient: 2234 ms (total 2234 ms)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm(25069): GC_FOR_MALLOC freed 357 objects / 778680 bytes in 198ms
W/System.err(25069): java.net.SocketException: Socket is closed
W/System.err(25069): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err(25069): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err(25069): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err(25069): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err(25069): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON3:
I/WindowManager( 110): home key test1= 1
D/WindowManager( 110): Home Key Test : false : false
D/WindowManager( 110): Home Key Test : false : false
I/ActivityManager( 110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher }
W/InputManagerService( 110): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45e1c7f8
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm(25069): GC_FOR_MALLOC freed 322 objects / 778768 bytes in 182ms
W/System.err(25069): java.net.SocketException: Socket is closed
W/System.err(25069): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err(25069): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err(25069): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err(25069): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err(25069): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON3:
I/ActivityManager( 110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.jtb.alogcat/.LogActivity }
D/dalvikvm( 1827): GC_FOR_MALLOC freed 8088 objects / 813080 bytes in 104ms
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm(25069): GC_FOR_MALLOC freed 131 objects / 770248 bytes in 186ms
W/System.err(25069): java.net.SocketException: Socket is closed
W/System.err(25069): at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err(25069): at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err(25069): at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err(25069): at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err(25069): at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON3:
onCreate()方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(LOG_TAG, "Before OnCreate() Try");
try {
Log.e(LOG_TAG, "In OnCreate() Try");
socket = new Socket("23.23.175.213", 9000);
Log.e(LOG_TAG, "Created Socket");
dataOutputStream = new DataOutputStream(socket.getOutputStream());
Log.e(LOG_TAG, "Created DataOutputStream");
dataInputStream = new DataInputStream(socket.getInputStream());
Log.e(LOG_TAG, "Created DataInputStream");
//out = new OutputStream();
out = socket.getOutputStream();
inputStr = socket.getInputStream();
//Thread readjsonthrd = new Thread(new ReadJSONThread());
//inputstrrd = new InputStreamReader(socket.getInputStream());
p = new Profile();
Log.e(LOG_TAG, "Created Profile Instance");
//Gets the local profile via JSON and converts into Profile type
Gson gson = new Gson();
Log.e(LOG_TAG, "Created Gson Instance" + "GetProfileJSONStr:" + p.getProfileJSONStr());
p = gson.fromJson(p.getProfileJSONStr(), Profile.class);
setProfile(p);
Log.e(LOG_TAG, "Converted Profile to JSON");
//Gson gson = new Gson();
Log.e(LOG_TAG, "Before: outputJSON = gson.toJson(p);");
outputJSON = gson.toJson(p).toString();
outputJSON = removeExcessStr(outputJSON);
Log.e(LOG_TAG, "ProfilePicStr Base64:"+p.getProfilePicStr());
outputJSON = outputJSON.replace("Name","name");
outputJSON = outputJSON.replace("TagLine","message");
outputJSON = outputJSON.replace("Title","title");
outputJSON = outputJSON.replace("Company", "company");
outputJSON = outputJSON.replace("Industry","industry");
outputJSON = outputJSON.replace("WhatIDo","whatido");
outputJSON = outputJSON.replace("WhoDoIWantToMeet","meetwho");
outputJSON = outputJSON.replace("WHOOZNEAR_PROFILEPIC","photo");
outputJSON = outputJSON.replaceAll("[c][o][n][t][e][n][t][:][/][/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+", getPicBase64Str()); /*"helloworld2"*/
if (!outputJSON.contains(",\"photo\":")) {
outputJSON = outputJSON.replace("}",",\"photo\":"+"\"IconnexUs\"}");
outputJSON = outputJSON.replace("}",",\"photo\":"+"\""+getPicBase64Str()+"\"}");
outputJSON = outputJSON.replace("}",",\"status\":\"enabled\"}");
}
else {
outputJSON = outputJSON.replace("}",",\"status\":\"enabled\"");
}
outputJSONserv = "{\"to\":\"broadcast\",\"type\":\"1\",\"payload\":"+outputJSON+"}";
Log.e(LOG_TAG, "Created outputJSON:" + outputJSON);
Log.e(LOG_TAG, "Created outputJSON Server:" + outputJSONserv);
JSONObject outObject = new JSONObject();
try {
outObject.put("photo", "hello");
outObject.put("type", "50");
outObject.put("payload", outputJSON);
outputJSON = gson.toJson(outObject).toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(LOG_TAG, "Value of PROFILEPIC STRING FROM PROFILEMAP: "+profileMap.get("WHOOZNEAR_PROFILEPIC"));
p.setProfilePicStr(ConvertandSetImagetoBase64(profileMap.get("WHOOZNEAR_PROFILEPIC")));
//String headerJSON = gson.toJson(outObject).toString();
outputJSON = outputJSON.substring(nthOccurrence(outputJSON, '{', 2)-1, nthOccurrence(outputJSON, '}', 1)-1);
String input = "["+"Ryan"+"[";
//"[foo".replaceAll(Pattern.quote("["), "\"");
String result = input.replaceAll(Pattern.quote("["), "\"");
Log.e(LOG_TAG, "REGEX REPLACEALL:"+result);
outputstrwr = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
outputstrwr.write(outputJSONserv);
Log.e(LOG_TAG, "Base64 String:"+p.getProfilePicStr());
Log.e(LOG_TAG, "Sent dataOutputStream");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(LOG_TAG, "Before initEventHandlers");
initEventHandlers();
Log.e(LOG_TAG, "After initEventHandlers");
//refreshViewModels();
/*Log.e(LOG_TAG, "Start Repeat Thread");
rt = new Thread(new RepeatingThread());
//rt = new RepeatingThread();
rt.start();
Log.e(LOG_TAG, "Started Repeat Thread");*/
Log.e(LOG_TAG, "Start Repeat Timer");
TimerTask task = new RepeatingTask();
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 3000);
Log.e(LOG_TAG, "Started Repeat Timer");
}
我的计时器任务:
public class RepeatingTask extends TimerTask {
//private int len = 0;
//private byte[] input = new byte[len];
public RepeatingTask() {
Log.e(LOG_TAG, "In RepeatingTask()");
Log.e(LOG_TAG, "Before inputJSON String");
String hello = "hello world";
//String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));
try {
Log.e(LOG_TAG, "Hello String:"+inputStr);
inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
Log.e(LOG_TAG, "Hello String2:"+inputJSON);
sendBytes(ConvertStringToByteArray(outputJSONserv), 0, ConvertStringToByteArray(outputJSONserv).length);
Log.e(LOG_TAG, "Hello String3:"+hello);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Convert
Log.e(LOG_TAG, "After inputJSON String:" + inputJSON);
//LOOK HERE FIRST
//inputJSON is what is received back from the server - Take the inputJSON
//String and use regular expressions HERE to remove all the other characters in the
//string except the payload JSON.
//refreshViewModels(inputJSON);
}
@Override
public void run() {
/*try {
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
//outputstrwr.write(outputJSONserv); //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
//inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
refreshViewModels(inputJSON);*/
try {
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
//outputstrwr.write(outputJSONserv); //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
//byte[] = myByteArray = readBytes(inputStr);
sendBytes(ConvertStringToByteArray(outputJSONserv), 0, ConvertStringToByteArray(outputJSONserv).length);
//sendBytes(myByteArray, 0, myByteArray.length);
Log.e(LOG_TAG, "AFTER SENDING DATA");
//inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON3:" + inputJSON);
refreshViewModels(inputJSON); //UNCOMMENT THIS AFTER I GET IT WORKING
}
}
refreshViewModels()方法:
public void refreshViewModels(String inputJSON) {
try {
ListView servicesListView = (ListView) this
.findViewById(R.id.profilesListView);
String[] from = new String[] { "profilePic", "neighborName",
"tagLine" };
int[] to = new int[] { R.id.avatar, R.id.username, R.id.email };
// prepare the list of all records
List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();
List<Profile> profiles = new ArrayList<Profile>();
// Clear the position mapping list and reset it
this.posMap.clear();
Log.i(LOG_TAG, "NEW inputJSON: " + inputJSON);
inputJSON = getPayloadStr(inputJSON);
Log.i(LOG_TAG, "NEW inputJSON2: " + inputJSON);
JSONArray profileArray = new JSONArray(inputJSON);
for (int i=0; i<profileArray.length(); i++) {
JSONObject jsonObject = profileArray.getJSONObject(i);
Gson gson = new Gson();
Profile p = gson.fromJson(gson.toJson(jsonObject).toString(), Profile.class);
profiles.add(p);
}
int pos = 0;
// Creates the fillMaps list for the listAdapter
Log.i(LOG_TAG, "Profiles size: " + profiles.size());
//showToast("Profiles size: " + profiles.size());
for (Profile p : profiles) {
// Create mapping for list adapter
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("profilePic",
p.getAttributeValue("photo")== null? "Not Set" : p
.getAttributeValue("photo"));
map.put("neighborName",
p.getAttributeValue("Name") == null? "Not Set" : p
.getAttributeValue("Name"));
map.put("tagLine",
p.getAttributeValue("TagLine") == null? "Not Set" : p
.getAttributeValue("TagLine"));
fillMaps.add(map);
// Reset the postion mapping
this.posMap.put(pos++, p);
}
ListAdapter servicesListAdapter = new myAdapter(this, fillMaps,
R.layout.listitem, from, to);
servicesListView.setAdapter(servicesListAdapter);
} catch (Exception e) {
Log.e(LOG_TAG, "Error making list adapter: " + e.getMessage());
}
}
以下是我的转化方法:
public String ConvertByteArrayToString(byte[] b) {
// byte[] to string
String input = new String(b);
return input;
}
public byte[] ConvertStringToByteArray(String str) {
// string to byte[]
byte[] bytes = str.getBytes();
return bytes;
}
以下是我用来发送和接收json字符串的方法:
public void sendBytes(byte[] myByteArray, int start, int len) throws IOException {
if (len < 0)
throw new IllegalArgumentException("Negative length not allowed");
if (start < 0 || start >= myByteArray.length)
throw new IndexOutOfBoundsException("Out of bounds: " + start);
// Other checks if needed.
// May be better to save the streams in the support class;
// just like the socket variable.
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(len);
if (len > 0) {
dos.write(myByteArray, start, len);
}
//OutputStream out = socket.getOutputStream(); //COMMENTED THIS OUT
//DataOutputStream dos = new DataOutputStream(out); //COMMENTED THIS OUT
/*dataOutputStream.writeInt(len); //COMMENTED THIS OUT
if (len > 0) {
dataOutputStream.write(myByteArray, start, len);
}*/
}
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
// Get the size of the file
int length = is.available();
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely stream ");
}
// Close the input stream and return bytes
is.close();
return bytes;
}
任何有关正确方向的帮助或指示都将受到赞赏。通过聊天提供的任何帮助也将非常感激。如果需要,可以在聊天会话中发布或查看整个文件。
答案 0 :(得分:0)
SocketException: socket is closed
表示你关闭套接字,然后继续使用它。这是你代码中的一个错误。您可能正在关闭其中一个流,然后仍尝试使用另一个流:关闭任一流将关闭另一个流和套接字。
您也在滥用available():请参阅Javadoc。它没有用你正在使用它,你在Javadoc中特别指出它是无效的。